Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean operators in a switch statement?

Tags:

javascript

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');
}
like image 980
Greg Avatar asked Jul 12 '10 04:07

Greg


1 Answers

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.

like image 62
eruciform Avatar answered Oct 12 '22 18:10

eruciform