Is it possible to test multiple conditions in a javascript switch statement? For example:
switch (var1 && var2) {
case var1 == 1 || var2 == 1:
alert("1");
break;
case var1 == 2 || var2 == 2:
alert("2");
break;
//etc...
}
The reason I am asking is because this would be a lot easier than using tons of if or switch statements.
Is it possible to test multiple conditions in a javascript switch statement? For example:
switch (var1 && var2) {
case var1 == 1 || var2 == 1:
alert("1");
break;
case var1 == 2 || var2 == 2:
alert("2");
break;
//etc...
}
The reason I am asking is because this would be a lot easier than using tons of if or switch statements.
Share Improve this question edited Feb 8, 2019 at 0:07 tadman 212k23 gold badges236 silver badges265 bronze badges asked Feb 8, 2019 at 0:01 FireFire 412 silver badges7 bronze badges4 Answers
Reset to default 4Update: Time goes and we receive new information. So here is an example of testing of multiple conditions in switch:
let var1 = 2;
let var2 = 2;
switch(true) {
case var1 === 1 || var2 === 1:
console.log('1');
break;
case var1 === 2 || var2 === 2:
console.log('2');
break;
}
This tricky approach was found in You Don't Know JS: Types & Grammar. I highly remend the whole series by the way.
Original Answer: No. It is not possible. Please see switch statement documentation.
JavaScript's switch
is fairly limited in what it can do, but that's not to say you can't get creative:
switch ([ var1, var2 ].join(',')) {
case '1,1':
alert("1");
break;
case '2,2':
alert("2");
break;
}
Here bining the two values with something like a ma means you have a single value you can then switch
on. The switch
itself can handle several different matches for the same branch, but the matches have to be very simple:
case '1,1':
case '1,2':
case '1,3':
// ...
break;
Where here any of those will follow the same branch.
If you're looking for something where either var1
or var2
can be a particular value, then it's time for an alternate approach using a look-up table:
let alerts = {
'1': '1',
'2': '2'
};
let var1 = 3;
let var2 = 2;
let hit = [ var1, var2 ].some(v => {
if (alerts[v]) {
alert(alerts[v]);
return true;
}
});
Here you can use some
to find the first matching value in your array of possible values to test.
Remember that within the context of switch
each condition is evaluated before the switching happens. It isn't evaluated like an if
is because switch
is meant to follow one branch and one branch only. if
chains necessarily go from one to the next until one triggers.
Nope, switch statements don't work this way. This won't throw an error, but it won't produce your intended results. (var1 && var2) evaluates to true. So your first case is testing for True == (var1==1 || var 2==1).
Instead you could maybe do nested statements:
switch (var1){
case 1:
switch (var2){
case 1:
alert ("1")
break;
}
// ...
}
//...
However in this case, I would opt for nested if statements for clarity
It's not posible, because it will need to validate more than one value and that is not supporter by switch statement.
My advice would it be: separate the "or" conditions and create some x number of functions and then call them from each separate case statement as you need.
If you need to do the same in several case statements, call the same function on each case statement.
Regards!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744182932a4562067.html
评论列表(0条)