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 syntax
Section titled “Switch syntax”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.');}The break statement
Section titled “The break statement”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.
Fall-through: one intentional use
Section titled “Fall-through: one intentional use”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.
The default case
Section titled “The default case”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.
Switch vs if/else
Section titled “Switch vs if/else”Use switch when | Use if/else when |
|---|---|
| Comparing one expression against many exact values | Checking ranges or complex conditions |
| Multiple cases share the same code (fall-through) | Logic involves &&, ` |
Code clarity improves over a wall of else if | Only two or three branches |
// switch is cleaner hereswitch (difficulty) { ... }
// if/else is better here (range checks)if (price < 100) { ... }else if (price < 200) { ... }Exercise
Section titled “Exercise”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 matchingcase.- Cases use strict equality (
===) — type coercion does not apply. breakexits 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. defaultis the catch-all fallback when no case matches, analogous to the finalelse.- Use
switchfor one-expression-against-many-values comparisons. Useif/elsefor ranges, complex conditions, and boolean logic.