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

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


Just put them right after each other without a break

switch (myVar) {  case 68:  case 40:    // Do stuff  break;  case 30:    // Do stuff  break;}


Yes, you just put the related case statements next to each other, like this:

case 40:  // Fallthroughcase 68:   // Do something   break;case 30:   // Do something different   break;

The Fallthrough comment is there for two reasons:

  • It reassures human readers that you're doing this deliberately
  • It silences warnings from Lint-like tools that issue warnings about possible accidental fallthrough.