3 ms·
> A better question is: which "case" expressions are evaluated - all of them, or only the ones enumerated before the match was found? Good point. By the way th
by lifthrasiir 6y ago
> A better question is: which "case" expressions are evaluated - all of them, or only the ones enumerated before the match was found?
Good point. By the way the answer is the latter, so the following prints 1 and 2.
switch (true) {
case (console.log(1), false):
case (console.log(2), true):
case (console.log(3), false):
break;
}
- Kinrany 6y agoEasy enough to remember because this is the simplest thing the language can do, but yeah, I have no idea if this is the same between popular languages.
- int_19h 6y agoIn the non-Boolean case, a common implementation technique for a large switch with sequential values is a jump table. But that's only possible when all case labels are known.