Skip to content
Advertisement

JavaScript shorthand ternary operator

I know that in PHP 5.3 instead of using this redundant ternary operator syntax:

JavaScript

…we can use a shorthand syntax for our ternary operators where applicable:

JavaScript

And I know about the ternary operator in JavaScript:

JavaScript

…but is there a shorthand?

Advertisement

Answer

JavaScript

Something like that what you’re looking for, where it defaults if undefined?

JavaScript

By the way, this works for a lot of scenarios, including objects:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement