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,247
  • 3
  • 15
  • 32
Mark
  • 4,773
  • 8
  • 53
  • 91
  • 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,771
  • 4
  • 41
  • 78
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
  • 22,603
  • 38
  • 120
  • 207