Switch Picks One Branch From Many
switch is a control-flow statement for when you're comparing a single value against a handful of known options. A chain of if/else if/else can do the same job, but once you're checking the same variable three or four times in a row, switch reads better.
The basic shape:
Reading that top-to-bottom: JavaScript evaluates day, compares it to each case label, and runs the matching block. default catches anything that didn't match, like a final else. The order of reading is value → case → code.
How the Matching Actually Works
switch compares with strict equality — the === operator. No type coercion, no fuzzy matches. case 1: will match the number 1 but not the string "1".
Only the second case matches. If you're ever surprised that your switch isn't triggering, the usual culprit is a string-vs-number mismatch coming from a form input or URL parameter.
break Is What Stops the Flow
This is the part that trips people up. A case label is a jump target, not a self-contained block. Once execution lands on a matching case, it keeps running line by line — straight through the next case's label — until it hits break, return, or the end of the switch.
Watch what happens without break:
The match is "editor", but you still get can edit, can view, and logged in. That's fallthrough — execution fell through each following case because nothing told it to stop.
Add break and it behaves:
Habit: every case ends with break unless you specifically want fallthrough.
Deliberate Fallthrough for Grouping
Fallthrough isn't always a mistake. Omitting break is a clean way to say "these cases all do the same thing":
Stacked empty case labels share the block below them. Notice there's no break needed after return — the function exits entirely. return inside a switch skips past everything that would have followed.
When you do want fallthrough between non-empty cases, leave a comment so the next reader doesn't "fix" it:
default Doesn't Have to Come Last
By convention default goes at the bottom, and that's where most readers expect it. But technically it's just another label — it matches when no case did, wherever it sits. If you put default in the middle without break, it'll fall through into whatever's below it. Put it last, end it with break (or don't, since nothing follows), and your future self will thank you.
Scoping Variables Inside a Case
Cases share one block scope — the whole switch. Declaring let or const with the same name in two cases throws a redeclaration error:
switch (x) {
case 1:
let msg = "one";
break;
case 2:
let msg = "two"; // SyntaxError: Identifier 'msg' has already been declared
break;
}
The fix is a block per case — wrap the body in { }:
Now each msg lives in its own scope and the collision goes away. This is worth remembering any time a case needs more than a line or two of logic.
switch vs if/else
switch shines when you're testing one value against many fixed options: HTTP codes, Redux action types, command names, enum-like strings. The value shows up once at the top, and the reader's eye can scan the case labels like a table of contents.
if/else is better when:
- You're comparing ranges (
score >= 90,score >= 75). - The conditions involve different variables or boolean expressions.
- You want loose equality or custom comparisons (
switchis always===).
A common modern alternative for the first pattern is an object lookup:
Three lines instead of a nine-line switch. When the cases are pure data mappings with no real logic, an object (or a Map) usually reads better. Reach for switch when each case does actual work.
Next: for Loops
switch handles one decision against many options. The next piece of control flow is repetition — running the same block of code over and over. That's what for loops are for, coming up next.
Frequently Asked Questions
How does a switch statement work in JavaScript?
switch compares a single value against a series of case labels using strict equality (===). When it finds a match, it runs the code for that case and keeps running until it hits a break or the end of the block. An optional default clause runs if no case matches.
What is fallthrough in a JavaScript switch?
If a case doesn't end with break (or return), execution keeps falling into the next case's code — even though its label didn't match. That's sometimes useful for grouping cases that share logic, but more often it's a bug. Always end cases with break unless you want fallthrough on purpose.
When should I use switch instead of if/else in JavaScript?
Use switch when you're comparing one value against many fixed options — HTTP status codes, action types, string commands. Use if/else when you need ranges, multiple variables, or boolean expressions. Remember that switch uses strict equality, so case '1' won't match the number 1.