I found a nasty copy-paste bug in a file I was debugging. It's a duplicate case label in a switch
statement.
switch (value) {
case 1:
case 2:
doSomething();
break;
case 2:
doSomethingElse();
break;
default:
break;
}
As it turns out, this is perfectly legal JavaScript. Not even the standard code quality tools complain here: JSHint and JSCS are fine with the repeated label, the missing break statement and the dead code after the second case 2.
To my knowledge, duplicate case values are illegal in C++ and Java.
Is there a purpose for having multiple identical case labels in a switch block in JavaScript? Why is this allowed?