admin管理员组

文章数量:1326641

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.

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.

Share Improve this question edited May 23, 2017 at 11:51 CommunityBot 11 silver badge asked May 27, 2015 at 0:21 shenkwenshenkwen 3,88010 gold badges54 silver badges101 bronze badges 4
  • 5 Add the default case logic in a function and call it from both places, is how i would do that. – Shan Robertson Commented May 27, 2015 at 0:30
  • 1 Make a method and call it.... – epascarello Commented May 27, 2015 at 0: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. – user4698813 Commented May 27, 2015 at 0: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 Commented May 27, 2015 at 4:29
Add a ment  | 

3 Answers 3

Reset to default 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'

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

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
}

本文标签: javascript go to the default from one case in switch statementStack Overflow