Missing Break in Switch
Quick summary
You forgot a break in a switch case, so execution falls through to the next case and runs code you did not intend.
Why this happens
Missing break statement at the end of a case
Assuming switch cases stop automatically (they do not)
Intentional fall-through without a comment (looks like a mistake)
Minimal example
switch (day) {
case 'Monday':
console.log('Start of week');
// Missing break!
case 'Friday':
console.log('Almost weekend');
break;
}
// For 'Monday', both messages print!switch (day) {
case 'Monday':
console.log('Start of week');
break; // Added break
case 'Friday':
console.log('Almost weekend');
break;
}
// For 'Monday', only 'Start of week' printsWithout break, execution falls through from Monday to Friday, printing both messages. Adding break after each case stops fall-through and only the matching case runs.
How to diagnose
Check if multiple cases' code runs when only one should
Look for missing break statements in switch cases
Add a default case to catch unexpected values
How to fix
Add break at the end of each case
If fall-through is intentional, add a comment explaining why
Consider using if/else instead of switch for clearer logic
How to prevent
Always add break to each case unless fall-through is intentional
Use a linter that detects missing break statements
Related Resources
Related Glossary
Related Lessons
- Conditions
Learn about switch statements.