2 ms·
Most of the time when I use switches, I'm returning from inside the case, because I tend to pull the switch out into another function. It's usually a good idea
by douche 10y ago
Most of the time when I use switches, I'm returning from inside the case, because I tend to pull the switch out into another function.
It's usually a good idea to create a new scope with your cases, so you can avoid some irritating variable name clashes, since, unlike and if, switch cases don't automatically do that, i.e.
switch(foo) {
case 0:
var bar = 2;
// do stuff...
break;
case 1:
var bar = 47; // error, conflicting variable name
// do stuff...
break;
}