JavaScript switch Statement
Learn the JavaScript switch statement: syntax, case, break, default, intentional fall-through, strict (===) matching, and block scope in cases.
Introduction to the switch Statement
In JavaScript, the switch statement runs one of several blocks of code depending on the value of a single expression. When you find yourself writing a long chain of if / else if statements that all compare the same variable against different values, a switch is usually a cleaner, more readable way to express the same logic.
This chapter covers the full syntax, how case, break, and default work together, intentional vs. accidental fall-through, the strict (===) matching switch uses, how to scope variables inside a case, and when switch is — and isn't — the right choice.
How switch works
A switch evaluates its expression once, then compares the result against each case value from top to bottom using strict comparison (===). The first matching case becomes the entry point: execution starts there and keeps running through the following statements until it hits a break, a return, or the end of the switch. If no case matches, the optional default block runs.
Syntax and Structure of the switch Statement
The skeleton of a switch statement looks like this:
switch(expression) {
case value1:
//Statements executed when the
//result of expression matches value1
break;
case value2:
//Statements for value2
break;
default:
//Statements executed if no case matches
}A few rules worth remembering:
- The expression is evaluated only once, at the top.
caselabels must be followed by a colon (:), not a brace.breakends theswitch; without it, execution falls through to the next case (see Fall-Through below).defaultis optional and does not have to be last, though placing it last is the convention.
Example of a Basic switch
Fall-Through: Grouping vs. Accidental
When a case has no break, execution "falls through" into the next case. This is a single mechanism with two very different outcomes — one useful, one a classic bug.
Intentional Grouping (good fall-through)
By stacking empty case labels, you can run the same code for several values. This is the idiomatic way to handle "any of these inputs should do the same thing."
Because 'apple' matches the first label and that label has no body, execution flows into the shared console.log('Red fruit') and then stops at break.
Accidental Fall-Through (the bug)
If you forget a break, every case after the match also runs until the next break or the end of the switch. Here fruit is 'apple', yet all four lines print:
Adding break; after each console.log would limit the output to just Apple is red.. The rule of thumb: end every case with break (or return) unless you are deliberately grouping cases.
Block Scope Inside a case
All case clauses share one block scope — the body of the switch. That means a let or const declared in one case is visible to the others and can cause a "Cannot access before initialization" error. Wrap a case's body in its own braces { ... } to give it a private scope:
Without the braces, declaring const result in both cases would throw a syntax error because of the duplicate declaration in the shared scope.
Strict (===) Matching in switch
JavaScript's switch compares the expression to each case value with strict equality (===) — no type coercion happens. So the type must match too, not just the value. For more on how === differs from ==, see Comparison Operators.
In this example the string '0' does not match the number 0, so the default runs:
If you need to match regardless of type, convert the expression first, e.g. switch (Number(x)).
switch vs. if/else
Both can express the same branching logic, so pick based on the shape of the condition:
- Prefer
switchwhen you compare one value against many discrete, constant options (a status code, a command name, a menu choice). It reads cleanly and the intent is obvious. - Prefer
if / else ifwhen conditions are ranges or boolean expressions (age >= 18,score > 90 && passed), involve different variables, or need the loose flexibility that strict===can't provide. - For mapping a key to a single value, a plain object lookup (
const labels = { add: 'Addition' }) is often shorter than either. See Logical Operators for combining conditions in theifstyle.
Conclusion
The switch statement in JavaScript is a versatile tool for handling multiple conditions. Understanding its proper use and nuances can greatly enhance the readability and efficiency of your code.