I was looking at code in the dark places of the internet when I came across the following way to use blocks on a switch.
input = -1;
switch(input){
case 1: case -1:
console.log("1 o -1");
break;
default:
console.log("Default")
break;
}
I've never seen this way of using a case
and it occurred to me that replacing the two cases with an operator OR
of the following form might work.
input = -1;
switch(input){
case 1 || -1:
console.log("1 o -1");
break;
default:
console.log("Default")
break;
}
I understand that if the input is 1
or -1
, it should return true and enter the first block, but if it is -1
, it jumps to the default block.
Why is this behavior and why is it not correct?