Re:If-else vs switch – Which is better?
by TweedHeads on Sunday August 02, 2009 @11:13PM
(#738513)
Sometimes I have to use a hack in javascript to create a quick switch, instead of if...else:
or switch:
I'll use:
I hope you find it useful in some cases too
[ Parent
| on HN ]
This is similar to the accepted idiom in python as well: create a dictionary mapping choices to outcomes.
For the special case of sequential numerical choices, a list works as well.
For the special-special case of numerical choices for single-word string outcomes, I often use:
dir = 'north eash south west'.split()[n]