Greg
Greg

Reputation: 7922

Boolean operators in a switch statement?

Is there any way to make this work with a switch, syntactically?

switch(i){
    case ('foo' || 'bar'):
        alert('foo or bar');
        break;
    default:
        alert('not foo or bar');
}

Upvotes: 7

Views: 3046

Answers (8)

Daniel Vassallo
Daniel Vassallo

Reputation: 344431

The problem with your example is that the expression ('foo' || 'bar') evaluates to 'foo', and therefore it will only match when i is 'foo', and never when i is 'bar'.

The || operator produces the value of its first operand if the first operand is truthy. Otherwise it produces the value of the second operand. A non-empty string is always truthy, and that's why your expression is returning 'foo'.

However, in JavaScript each case falls through into the next case unless you explicitly disrupt the flow with a break or a return. Therefore you can easily make it work as follows:

switch(i) {
    case 'foo':
    case 'bar':
        alert('foo or bar');
        break;
    default:
        alert('not foo or bar');
}

Upvotes: 1

John K
John K

Reputation: 28897

From the official Mozilla Developer Center docs, use multiple cases like so:

 // multiple cases:      

   case "Mangoes":  
   case "Papayas":  
      document.write("Mangoes and papayas are $2.79 a pound.<br>");  
      break;  
   default:  
      document.write("Sorry, we are out of " + expr + ".<br>");  
 }

Or if looking for an IE solution you would use the JScript docs for switch which refer to case conditions as "labels" and states:

multiple label blocks are executed if a break statement is not used.

Effectively both sets of documentation say the same thing about putting multiple cases together.

Upvotes: 5

Adam P
Adam P

Reputation: 4703

You have to set separate cases for each value.

switch(i)
{ 
    case 'foo': 
    case 'bar': 
        alert('foo or bar'); 
        break; 
    default: 
        alert('not foo or bar'); 
}

Using your code, you'll only get the alert if i evaluates to true, since a string that is not empty also evaluates to true.

Upvotes: 1

J.C.
J.C.

Reputation: 208

switch(i)
{
    case 'foo':
    case 'bar':
        alert('foo or bar');
        break;
    default:
        alert('not foo or bar');
        break;
}

Upvotes: 1

user387302
user387302

Reputation: 405

Would something along this line work?

switch(i){ 
    case ('foo'):
    case ('bar'): 
        alert('foo or bar'); 
        break; 
    default: 
        alert('not foo or bar'); 
} 

Upvotes: 1

kmoser
kmoser

Reputation: 9283

switch(i){
    case 'foo':
    case 'bar':
        alert('foo or bar');
        break;
    default:
        alert('not foo or bar');
}

Upvotes: 1

eruciform
eruciform

Reputation: 7736

switch(i){
  case 'foo':
  case 'bar':
    alert('foo or bar');
    break;
  case 'other':
  default:
    alert('other');
}

Note: "other" isn't required, I'm just showing that you can stack cases with default as well.

Upvotes: 13

staticsan
staticsan

Reputation: 30555

JavaScript doesn't work that way. Do it like this:

switch(i){
    case 'foo':
    case 'bar':
        alert('foo or bar');
        break;
    default:
        alert('not foo or bar');
}

Like in C, JavaScript's case clause will cascade.

Upvotes: 3

Related Questions