3 ms·
Stop using (nested) ternary operator's ! Use if-statements! if(val==1) var res = 1; if(val==2) var res = 2;
by z3t4 9y ago
Stop using (nested) ternary operator's ! Use if-statements!
if(val==1) var res = 1;
if(val==2) var res = 2;
- 11235813213455 9y agoYou're joking right? first: `var` is completely outdated, second: it should be `else if` for the next conditions, and third: ternary, nested or not, is simply better for that
- Kerrick 9y agoThere's real value in the fact that ternary operators form expressions, which if statements don't.
- z3t4 9y agoCould you elaborate ? I think the only difference is readability, where I favor if-statements over expressions.
- Epenthesis 9y agoExcept ternary expressions have the huge advantage that they allow assigning to const. Eg: const var = val == 1 ? 1 : va1 == 2 ? 2 : null;
- z3t4 9y agoIt's hard to argue about not using const, but can you remember any time where const actually prevented a bug !?