Y
Hacker News
new
|
ask
|
show
|
jobs
by
z3t4
2985 days ago
Stop using (nested) ternary operator's ! Use if-statements!
if(val==1) var res = 1; if(val==2) var res = 2;
2 comments
Epenthesis
2985 days ago
Except ternary expressions have the
huge
advantage that they allow assigning to
const
. Eg:
const var = val == 1 ? 1 : va1 == 2 ? 2 : null;
link
z3t4
2985 days ago
It's hard to argue about not using const, but can you remember any time where const actually prevented a bug !?
link
Kerrick
2985 days ago
There's real value in the fact that ternary operators form
expressions
, which if statements don't.
link
z3t4
2985 days ago
Could you elaborate ? I think the only difference is readability, where I favor if-statements over expressions.
link