Switch
switch compares one value against a list of case labels using strict equality, then runs the matching arm. Without break or return, execution falls through into the next case.
Detailed Explanation
A switch is a multi-way branch. You evaluate an expression once, then walk the case labels from top to bottom. The first label that is === to that value wins. If none match, default runs, and default can sit anywhere in the list; it is not required to be last.
The comparison is strict. switch (1) will not match case "1":. There is no ToNumber or ToString coercion between the discriminant and the labels. If you need fuzzy matching, switch is the wrong tool.
The feature that surprises people is fall-through. When a case matches, JavaScript starts executing statements from that point and keeps going into later cases until it hits break, return, throw, or the end of the switch. That is how you share a body across several labels:
case "save":
case "draft":
persist();
break;
It is also how you silently run three handlers when you meant one. Always put a break (or return) at the end of each arm unless you are grouping labels on purpose, and leave a comment on intentional fall-through.
case labels are expressions, not just literals. case compute(): is legal and is evaluated when the switch reaches that label, not in advance. Side effects in labels make the order of cases observable, so keep labels cheap and pure.
switch is a statement. It does not produce a value. If you want an expression, assign inside each arm or use a lookup object / Map instead. A lookup is often clearer when every arm is "return this constant".
switch (true) is a pattern you will see: each case is a boolean expression, and the first truthy one wins. It works because true === true. It is harder to read than if / else if and is rarely worth it.
Reach for switch when one value has a handful of known constants and each constant needs different work. Reach for an object map when the work is "pick a value". Reach for if when the tests are ranges, inequalities, or overlapping predicates.
What it costs you: fall-through bugs, and the inability to match ranges. case n > 10: does not mean "when n is greater than 10"; it means "when the discriminant is strictly equal to the boolean n > 10".
Code Examples
function httpLabel(status) {
switch (status) {
case 200:
case 201:
return "success";
case 301:
case 302:
return "redirect";
case 400:
case 404:
return "client error";
case 500:
return "server error";
default:
return "other";
}
}
console.log(httpLabel(201)); // => success
console.log(httpLabel(404)); // => client error
console.log(httpLabel("200")); // => other (strict equality, string !== number)let log = [];
const action = "save";
switch (action) {
case "save":
log.push("persist");
// missing break: falls through on purpose? here it is a bug
case "notify":
log.push("email");
break;
default:
log.push("ignore");
}
console.log(log); // => [ 'persist', 'email' ]Common Gotchas
Cases fall through without `break`
forgetting `break` runs every later arm. Group labels only when they share one body, and comment that intent.
Matching is `===`, not `==`
`switch (1)` misses `case "1":`. Coerce the discriminant once if incoming data is mixed, or normalise types at the boundary.
`case n > 10` is not a range test
the label is an expression that yields a boolean. `switch (n)` will never equal that boolean unless `n` itself is `true` or `false`.
`default` can appear anywhere
it runs only when no `case` matched. Place it last unless you have a reason, so readers find it.