The ternary operator is called from within an expression and returns an expression.
While the if and the ternary do share a lot of the same characteristics on the surface, there are…
Joel Thoms
41

That’s not an excuse to use a ternary operator when the code’s semantics ask for an if/else statement.

Besides, this is just a limitation of Javascript — other languages allow if statements to be expressions as well. That said, there’s a “do expressions” proposal (at stage 1 currently) to add that in JS too.

Show your support

Clapping shows how much you appreciated Nickos Ventouras’s story.