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
102

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.

Like what you read? Give Nickos Ventouras a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.