DEV Community

Discussion on: Ternary Operators In JavaScript

Collapse
 
puritanic profile image
Darkø Tasevski

Ternary operators are not a shortcut nor replacement for If/else statements. Ternaries are okay when you need to return something based on condition, and you want to write it as oneliner. They are okayish in React too (as we can't use if/else in jsx). Besides this ternaries can get hard to read and hard to maintain pretty fast, so I think that overusing them would be a really bad idea.
There's even Eslint rule for disallowing nested ternaries 🙂

In the end, it's only important that a programmer knows what syntax to use in what situation.

Collapse
 
iandavid profile image
David Okpare

Yes, you're absolutely right! The article wasn't meant to "replace if statements", but shed light on it being a possible option for using conditional operators :) And thanks for highlighting the fact that it shouldn't be overused. I have included it in the post