Break in switch statement

The Switch Statement

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

What happens if I forgot a break?

If you forget a break then script will run from the case where criteria is met, and will run the case after that regardless if criteria was met. See example here:
var foo = 0;
switch (foo) {
  case -1:
    console.log('negative 1');
    break;
  case 0: 
    console.log(0);
    // NOTE: the forgotten break 
  case 1:     
    console.log(1);
    break; //  break 
  case 2:
    console.log(2);
    break;
}
Console output:
01

Read more: MDN: Switch

Follow CodeGalaxy

Mobile Beta

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