I know that in php 5.3 instead of using this redundant ternary operator syntax:
startingNum = startingNum ? startingNum : 1
...we can use a shorthand syntax for our ternary operators where applicable:
startingNum = startingNum ?: 1
And I know about the ternary operator in javascript:
startingNum = startingNum ? startingNum : 1
...but is there a shorthand?
Thanks guys!
||
will return the first truthy value it encounters, and can therefore be used as a coalescing operator, similar to C#'s??
To make a ternary like:
in javascript, you can do:
Example:
Yes, there is:
In general,
expr1 || expr2
works in the following way (as mentioned by the documentation):If you have a condition with null, like
you can use '&&'
In this case, you can use the OR operator.
The above answers are correct. In JavaScript, the following statement:
can be expressed as
Another scenario not covered here is if you want the value to return false when not matched. The JavaScript shorthand for this is:
But it can be expressed as
Just wanted to cover another scenario in case others were looking for a more generalized answer.