2

Can I put a conditional operator in my JS switch statement?

eg. case n||n:

eg. case "string2"||"string1":

switch(expression) {
  case n||n:
    code block
    break;
  case n||n:
    code block
    break;
  default:
    default code block
}
Audwin Oyong
  • 2,061
  • 3
  • 10
  • 31
Mark
  • 4,675
  • 8
  • 48
  • 90
  • 2
    Most people will do this: http://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript – Seiyria Apr 24 '15 at 17:21

2 Answers2

6

You can use a fall-through:

switch (expression) {
  case "exp1":
  case "exp2":
    // code block
    break;
  default:
    // default code block
}
Jonathan
  • 8,095
  • 4
  • 37
  • 70
2

Like this:

switch(expression) {
    case n:
    case n:
        code block
        break;
    case n:
    case n:
        code block
        break;
    default:
        default code block
} 

Basically lay them out one after the other

StudioTime
  • 20,816
  • 34
  • 110
  • 202