-1

Hi all I searched and found old answers like in 2008, im creating firefox addon for new browsers so i can use ecma 5+.

I was trying to do a a switch that had one block meet multiple criteria like if >=0 && <= 3 like below:

switch (blah) {
     case 0, 1, 2, 3: //<<<<<<<< this here please
          //do this;
          break;
     default:
          ///do this
}

This is just a basic example.

I would like multiple values to trigger the same case. How do I do this?

Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98
Noitidart
  • 35,443
  • 37
  • 154
  • 323

2 Answers2

9

This might be what you are looking for. If it is please more clearly define your problem/question.

switch (x) {
   case 0:
   case 1:
   case 2:
   case 3:
   // do this
   break;
   default:
   // do this
}

See also Multiple Cases in Switch:

Community
  • 1
  • 1
Joshua Enfield
  • 17,642
  • 10
  • 51
  • 98
  • 2
    ahhh superb! thanks for the super fast reply!! i cant accept soltuion for 9 minutes lol. this lets me get right back to my program! thx josh! – Noitidart Feb 16 '14 at 07:39
  • hey thx man i selected your solution and jeronimo's below but its not letting me select two solutions – Noitidart Feb 16 '14 at 08:03
  • 1
    This really is the better answer. It should have been accepted: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#Methods_for_Multi-criteria_Case – Martijn Jun 03 '14 at 15:05
4

There is one hacky switch style that allows you to specify ranges:

switch (true) {
    case blah >= 0 && blah <= 3:
        //do this
        break
    default:
        //do that
}

It sometimes useful in cases like this.

Maybe it'll do what you want.