4

How do I do this in Javascript?

switch(c){
          case 'a':
                 //do something, condition does not match so go to default case
                 //don't break in here, and don't allow fall through to other cases.
          case 'b':
                 //..
          case 'c':
                 //..
          case '_':
                 //...
          default:
                 //
                 break;
}

This is a almost a copy of Jumping from one case to the default case in switch statement, but there isn't a solution for javascript, so I have to add this new post.

Community
  • 1
  • 1
shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • 5
    Add the default case logic in a function and call it from both places, is how i would do that. – Shan Robertson May 27 '15 at 00:30
  • 1
    Make a method and call it.... – epascarello May 27 '15 at 00:35
  • Or you could wrap your `switch` inside of a function that takes the switch value as an argument, and if certain condition is met within case `'a'`, call the same function again, but with a value that you know will not match any of the cases. Either way, I wouldn't find that to be a great solution. –  May 27 '15 at 00:58
  • I don't think a switch statement would be best for what your attempting to solve. Wouldn't it be better to have a nested `if` with multiple `method` call points? If I had to use this method I would probably do as @ShanRobertson suggested. – lindsay May 27 '15 at 04:29

3 Answers3

2

You could always break in the default case and return outside the switch:

switch(c) {
  case 'a':
    if (something) return 'a'
    break
  case 'b':
    return 'b'
  default:
    break
}

return 'default stuff'
1

Another option which might work, depending on your use-case, is to move the default case out of the switch.

switch(c){
      case 'a':
             //do something, condition does not match so go to default case
             //don't break in here, and don't allow fall through to other cases.
      case 'b':
             //..
      case 'c':
             //..
      case '_':
             //...
}

// Do the `default/finally` here instead
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
0

I'm not sure if it's a good idea over nested if statements but this would satisfy your criteria.

It would only enter one case block, then if it satisfies an if statement you can return out of the whole switch statement, otherwise drop down to your default case.

switch(x) {
  case 1:
    if (something) {
      return
    }
  case 2:
    if (somethingElse) {
      return
    }
  default:
    return
}
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 23 '21 at 17:23