DEV Community

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

Collapse
 
timsev profile image
Tim Severien

I use the ternary operator a lot. I strongly believe it should be used with caution. Most of the time, I start with regular if-statements and reduce it to a ternary operator if I deem the condition and expressions simple enough, even though I realise that is extremely subjective. I never nest ternary operators.

value < threshold ? value : otherValue;

object.someState || object.someOtherState
  ? object.child.doSomething()
  : object.otherChild.doSomethingElse();