JavaScript, Typescript switch statement: way to run same code for two cases?

前端 未结 5 667
心在旅途
心在旅途 2020-12-29 02:22

Is there a way to assign two different case values to the same block of code without copy and pasting? For example, below 68 and 40 should execute the same code, while 30 is

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 02:32

    You should use:

    switch condition {
      case 1,2,3:
        // do something
      case 4,5:
        // do something
      default:
        // do something
    }
    

    Cases should be comma-separated.

提交回复
热议问题