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?
The behavior is as expected, it happens that for each block
case
you should use a statementbreak
that indicates the exit or interruption of theswitch
, if you don't, the evaluation of the next block will be executed.For example:
As can be seen, by not incorporating or including the statement
break
, the following block of code is executed regardless of whether it does not match the given case. The statementswitch
looks for the case and starts executing from the first one it finds within thecase
, if it doesn't get any, it executes the statementdefault
.If we put the sentence
break
the code is executed correctly:However, we see that for both case 1 and -1, the block to be executed is identical, so we can write:
And if you want to make it a bit more obvious, then you write it as you show in your question:
Why
case 1 || -1
doesn't it work?First of all, it must be understood that the statement
switch ... case
compares the value of the variable passed as an argument toswitch
the value declared incase
:So using one
case
of this type:case 1 || -1
will always evaluate the case when values is 1, because the operator||
is short-circuiting, and returns the leftmost element if itself evaluates totrue
, that is, if itself istruthy
.That means that it
case 1 || -1:
will always be equal tocase 1:
and will never be equal tocase -1:
We can see it:
Hope this clears your doubt. Agur!!!
The case statement syntax expects a single value, as its documentation says .
In your question you intend to compare the switch parameter with different case values, which is not interpreted as you intend, because what the case does is resolve that OR to convert it to a single value before being executed in the switch statement.
If you do this in javascript:
you will see that it returns 1, that's why it only works in that case.
To use conditionals within case statements, it is still possible, but they must be resolved before being compared to the case statement parameter.
In your case, this could be a solution:
where we establish a boolean value true as a parameter , and within the case we compare both possible inputs with the OR, which will return true or false, allowing us to enter the corresponding case or jump to the default.
The block
switch
compares the value of eachcase
withinput
. You can't use logical operators in acase
. In the example you gave it works withinput = 1
a particularity of the operator||
.1 || -1
is an expression that evaluates to1
(because 1 == true, so the operator||
returns the first value). Then the label remains ascase 1:
, that's why when you putinput = 1
enters that case but not withinput = -1