Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Stop using (nested) ternary operator's ! Use if-statements!

  if(val==1) var res = 1;
  if(val==2) var res = 2;


Except ternary expressions have the huge advantage that they allow assigning to const. Eg:

    const var =
      val == 1 ? 1 :
      va1 == 2 ? 2 :
      null;


It's hard to argue about not using const, but can you remember any time where const actually prevented a bug !?


There's real value in the fact that ternary operators form expressions, which if statements don't.


Could you elaborate ? I think the only difference is readability, where I favor if-statements over expressions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: