I want to use a variable in a switch case. as an example, could not do it. Can you help?
switch(basket,pay){
case true, true:
blah blah...
break;
case false,true:
blah blah..
break;
case false, false:
blah blah...
break;
}
I want to use a variable in a switch case. as an example, could not do it. Can you help?
switch(basket,pay){
case true, true:
blah blah...
break;
case false,true:
blah blah..
break;
case false, false:
blah blah...
break;
}
Share
Improve this question
edited Jun 28, 2012 at 0:23
Matt Greer
62k18 gold badges126 silver badges124 bronze badges
asked Jun 28, 2012 at 0:09
Escort42Escort42
511 silver badge4 bronze badges
1
- You can only switch on 1 value - basket or pay (not both) – Kane Commented Jun 28, 2012 at 0:12
5 Answers
Reset to default 12What you're looking for is flags and bitmasks. Basically you store myltiple pseudo-boolean values insdie the same variable and you can access their compound condition. The beauty of this is that you can compound multiple properties into a single variable. This is especially neat when there are a lot more boolean properties which you need to keep track of
var FLAG_BASKET = 0x1; // 0001
var FLAG_PAY = 0x2; // 0010
//set them both to pseudo-true
var flags = FLAG_BASKET | FLAG_PAY;
switch (flags){
case 0x0://both are false
case 0x1://basket is true, pay is false
case 0x2://basket is false, pay is true
case 0x3://both are true
}
Fiddled
the original problem can be written with switch
:
switch (true) {
case basket && pay:
// both true
break;
case basket && !pay:
// basket true, pay false
break;
case !basket && pay:
// basket false, pay true
break;
case !basket && !pay:
// basket false, pay false
break;
}
What you've written is legal JavaScript, but it doesn't do what you expect.
In an expression, the comma evaluates to the last member. This is commonly used in for loops to initialize two variables at the same time:
for(var i = 0, l = myArray.length; i < l; ++i) {
...
}
When you use the comma operator in your switch and case statements, the first value is discarded and the last value is used. See this fiddle for an example: http://jsfiddle.net/UG4vf/
It is not comparing both values and finding the case that matches both of them. In other words, your above code reduces to this:
switch(pay){
case true:
blah blah...
break;
case true:
blah blah..
break;
case false:
blah blah...
break;
}
Your switch statement doesn't seem necessary, to be entirely honest. You should be ok with some simple if
's:
if (basket && pay) {
// code
} else if (pay) {
// code
} else {
// code
}
With this solutions you can supply multiple arguments to a switch:
const a = "foo";
const b = true;
const c = null;
const condition = JSON.stringify({ a, b, c });
function mySwitch(condition) {
switch (condition) {
case JSON.stringify({
a: "foo",
b: true,
c: true
}):
return 'case { a: "foo", b: true, c: null }';
case JSON.stringify({
a: "foo",
b: true,
c: null
}):
return 'case { a: "foo", b: true, c: null }';
default:
return 'default';
}
}
console.log("condition:", condition);
console.log("result:", mySwitch(condition));
Besides an object as condition this also works with an array, however the downside of using this solution is that you have to pass the complete switch condition value to the switch case condition. For example if the switch case cares about properties a
and b
from the example and not c
then this solution won't work. But if you know for sure that c
will always have a specific value for your case then you could use this solution.
Because the OP's example code includes a repetitive amount of arguments for each case this solution may help you to hack in the possibility to supply a switch with more then one arguments.