switch / case
JavaScript · Reference cheat sheet
switch / case
JavaScript · Reference cheat sheet
📋 Overview
switch selects a branch by strict equality (===) against a discriminant. Cases fall through until break, return, or throw. Prefer maps/objects or early returns when branches share little structure.
🔧 Core concepts
- Syntax:
switch (expr) \{ case v: ... break; default: ... \}. - Matching: strict equality; no pattern matching built-in.
- Fall-through: intentional sharing of bodies; always comment intentional fall-through.
- Scope: wrap case bodies in
\{ \}when usinglet/constto avoid TDZ clashes. - Alternatives: lookup tables,
Map, if/else chains, pattern libs.
switch (status) {
case 200:
return "ok";
case 404:
return "missing";
default:
return "other";
}💡 Examples
function httpLabel(code) {
switch (code) {
case 200:
case 201:
case 204:
return "success";
case 301:
case 302:
return "redirect";
case 400:
case 401:
case 403:
case 404:
return "client-error";
case 500:
return "server-error";
default:
return "unknown";
}
}
// Block scope per case
switch (kind) {
case "user": {
const id = crypto.randomUUID();
return { kind, id };
}
case "guest": {
const id = "guest";
return { kind, id };
}
default:
throw new Error(`bad kind: ${kind}`);
}
// Dispatch table
const handlers = {
ping: () => "pong",
time: () => Date.now(),
};
const result = (handlers[cmd] ?? (() => "noop"))();
// switch (true) pattern (use sparingly)
switch (true) {
case score >= 90:
return "A";
case score >= 80:
return "B";
default:
return "C";
}// Exhaustiveness helper for unions (manual)
function assertNever(x) {
throw new Error(`unexpected: ${x}`);
}⚠️ Pitfalls
- Missing
breakcauses accidental fall-through bugs. casevalues are compared with===—"1"does not match1.let/constin a case without braces collide with other cases.- Large switches are harder to test — prefer strategy maps for open sets of commands.
defaultis optional but recommended for unexpected values.
🔗 Related
- boolean.md — conditions
- objects.md — dispatch tables
- map.md — Map-based dispatch
- error.md — default throws
- oop.md — polymorphic alternatives