DEV Community


Discussion on: What do you think about the ternary operator?

nimmo profile image

I'm a big fan of ternaries in JS, because IMO expressions are better than statements for reducing cognitive load, which is generally my primary goal when coding.

However, I find that my personal favourite formatting for ternaries is:

  ? valueIfTrue
  : valueIfFalse;

To be clear though, I do believe that if/else is better for readability (especially for less experienced developers), and I prefer languages which only have expressions (and consequently their if/elses are expressions too). But for languages that aren't as nice as that, I always prefer ternaries for assignments.