Multi-case - single operation

Multi-case - single operation

This method takes advantage of the fact that if there is no break below a case statement it will continue to execute the next case statement regardless if the case meets the criteria.
This is an example of a single operation sequential switch statement, where four different values perform exactly the same.
var Animal = 'Giraffe';
switch (Animal) {
  case 'Cow':
  case 'Giraffe':
  case 'Dog':
  case 'Pig':
    console.log('This animal will go on Noah\'s Ark.');
    break;
  case 'Dinosaur':
  default:
    console.log('This animal will not.');
}
Read more: Stackoverflow: Switch statement multiple cases in JavaScript

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
JavaScript snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes