JavaScript switch: case labels and default fallback
The JavaScript switch statement evaluates an expression once and routes execution to a matching case label using strict equality (===). Use it to replace if...else chains when every branch tests the same variable against discrete values — dispatching commands, mapping statuses, or handling fixed options. Switch groups this value-based control flow into one block with explicit fall-through behavior controlled by break.
JavaScript Switch Example For Value Routing
Output:
Output will appear here...
Output:
Saving file
How This Example Works
switch (command)evaluatescommandonce and compares the result against each JavaScript case label in source order.- The
"save"case matches, so execution jumps to that branch and runsconsole.log("Saving file"). breakexits the switch block. Without it, execution falls through into the next case body regardless of whether its label matches.defaultacts as the catch-all — it runs when no case label matches. Nobreakis needed afterdefaultwhen it appears last.
In a JavaScript case statement like case "save":, the label is a value to match, not a separate boolean condition.
Common Mistakes in JavaScript Switch Case
Forgetting break: Without it, execution falls through into every subsequent case body. Omit break only when grouping cases intentionally.
const command = "save";
switch (command) {
// Wrong — logs both messages when command is "save"
case "save":
console.log("Saving");
case "close":
console.log("Closing");
}
Mixing types in case labels: Switch uses ===, so "2" never matches 2. Coerce the expression to a consistent type before switching.
Reusing let/const across cases: All cases share the switch block scope. Two cases that declare let result cause SyntaxError — wrap each case in { } to create separate lexical scopes.
Switch vs if…else
Use switch when… | Use if...else when… |
|---|---|
| Comparing one expression against multiple fixed values | Conditions involve ranges, inequalities, or different variables |
| Multiple cases share the same handler via fall-through | Each branch has independent, compound logic |
For large value-to-action mappings, an object lookup can be cleaner than either approach. Guard missing keys so you don’t call undefined as a function.
More Examples
Group multiple case labels to share one handler:
const day = "Sat";
switch (day) {
case "Sat":
case "Sun":
console.log("Weekend");
break;
default:
console.log("Weekday");
}
Stacking case labels without break between them routes all listed values to the same block. Keep the shared handler short and end it with break, return, or throw to avoid accidentally running later cases.
If you need different behavior per label, avoid fall-through and give each case its own body and exit.
FAQ
Does JavaScript switch use == or ===?
Switch compares with strict equality (===). No type coercion occurs, so 0 does not match "0" and null does not match undefined. Ensure the expression and case labels share the same type.
What happens if you omit break in a switch case?
Execution falls through into the next case body regardless of the label. This continues until a break, return, or the end of the switch block. Intentional fall-through is used to group multiple case labels under one handler.
Can default appear before other cases?
Yes. default can be placed anywhere in the switch block. It still runs only when no case matches, then execution continues from that point — so add break after default unless it is the last clause.