Mark
Mark

Reputation: 4883

JavaScript Switch Statement - Condition in Case

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
}

Upvotes: 2

Views: 1309

Answers (2)

Jonathan
Jonathan

Reputation: 9151

You can use a fall-through:

switch (expression) {
  case "exp1":
  case "exp2":
    // code block
    break;
  default:
    // default code block
}

Upvotes: 6

StudioTime
StudioTime

Reputation: 24019

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

Upvotes: 2

Related Questions