Skip to content

Switch Statements

When a single value needs to be compared against many possible exact matches, a long chain of else if statements works but gets repetitive. A switch statement is designed exactly for this scenario.

switch (expression) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}

JavaScript evaluates the expression once, then looks for a case whose value matches exactly (using ===). When it finds a match, it starts executing code from that point.

const tourDifficulty = 'moderate';
switch (tourDifficulty) {
case 'easy':
console.log('Suitable for all fitness levels. Flat terrain, under 5 miles.');
break;
case 'moderate':
console.log('Some elevation gain. Recommended for active hikers.');
break;
case 'challenging':
console.log('Significant elevation and distance. Prior experience recommended.');
break;
case 'expert':
console.log('Strenuous terrain. Advanced fitness and gear required.');
break;
default:
console.log('Difficulty level not recognized.');
}

break exits the switch block after the matched case runs. Without it, execution falls through to the next case and continues running code — even code for a different case.

const tourDifficulty = 'easy';
switch (tourDifficulty) {
case 'easy':
console.log('Easy tour');
// no break — falls through to the next case
case 'moderate':
console.log('This runs even though difficulty is easy!');
break;
}

This is one of JavaScript’s most common beginner mistakes. Always add break after each case unless you specifically intend fall-through.

The one legitimate use of fall-through is when multiple cases should run the same code:

switch (tourDifficulty) {
case 'easy':
case 'moderate':
console.log('Family-friendly options available.');
break;
case 'challenging':
case 'expert':
console.log('Experienced hikers only.');
break;
}

Both 'easy' and 'moderate' fall through to the same log statement. This is intentional and clear because the cases are stacked immediately above the shared code.

default is the catch-all — it runs when no case value matches the expression:

switch (tourDifficulty) {
case 'easy':
console.log('Easy');
break;
case 'moderate':
console.log('Moderate');
break;
default:
console.log(`Unknown difficulty: ${tourDifficulty}`);
}

default is optional but usually worth including. It catches typos, unexpected values, and future data that does not match any existing case.

Use switch whenUse if/else when
Comparing one expression against many exact valuesChecking ranges or complex conditions
Multiple cases share the same code (fall-through)Logic involves &&, `
Code clarity improves over a wall of else ifOnly two or three branches
// switch is cleaner here
switch (difficulty) { ... }
// if/else is better here (range checks)
if (price < 100) { ... }
else if (price < 200) { ... }
const tourDifficulty = 'moderate';
switch (tourDifficulty) {
case 'easy':
console.log('Suitable for all fitness levels. Flat terrain, under 5 miles.');
break;
case 'moderate':
console.log('Some elevation gain. Recommended for active hikers.');
break;
case 'challenging':
console.log('Significant elevation and distance. Prior experience recommended.');
break;
case 'expert':
console.log('Strenuous terrain. Advanced fitness and gear required.');
break;
default:
console.log('Difficulty level not recognized.');
}

Test all four difficulty values and then assign an unrecognized value like 'beginner' to trigger the default case. Then intentionally remove one break to observe fall-through behavior.

  • switch(expression) evaluates the expression once and jumps to the matching case.
  • Cases use strict equality (===) — type coercion does not apply.
  • break exits the switch after a case runs. Without it, execution falls through to the next case.
  • Intentional fall-through — stacking cases above shared code — is the one acceptable use of omitting break.
  • default is the catch-all fallback when no case matches, analogous to the final else.
  • Use switch for one-expression-against-many-values comparisons. Use if/else for ranges, complex conditions, and boolean logic.