Chapter V · Control Flow

The switch Statement

switch compares one value against several possible matches, running the code attached to whichever case fits — a cleaner shape than a long else if chain for that specific pattern.

01 What Is the switch Statement?

switch evaluates a single expression once, then compares the result against each case value using strict equality (=== — no type coercion). The first matching case is where execution begins, and a default clause acts as the fallback when nothing matches, similar to a final else.

02 Simple Example

Routing behavior based on an HTTP status code.

switch.js
const status = 200;\n\nswitch (status) {\n  case 200:\n    console.log("OK");\n    break;\n  case 404:\n    console.log("Not Found");\n    break;\n  default:\n    console.log("Unknown Status");\n}

200 strictly equals the first case, so "OK" logs. The break immediately after stops execution from continuing into the 404 case below it.

03 How It Works — Fall-Through

Once a case matches, execution doesn't stop at the end of that case's block automatically — it keeps running into the next case, and the next, until it hits a break or reaches the end of the switch. This is called fall-through, and it's a deliberate ECMAScript design, not a bug: it lets multiple case labels share one block.

fall-through.js
switch ("b") {\n  case "a":\n  case "b":\n    console.log("a or b");\n    break;\n  case "c":\n    console.log("c");\n}\n// logs: "a or b"

Both "a" and "b" share the same block because there's no code — and no break — between the two case labels, so matching "b" falls straight through to the shared statement.

04 Step-by-Step

  1. Evaluate the switch expression exactly once.

  2. Compare the result against each case value with ===, top to bottom.

  3. On a match, start executing statements from that case onward.

  4. Continue executing through subsequent cases until a break, return, or the end of the switch.

  5. If nothing matched, run the default block if one exists.

05 Common Mistakes & Edge Cases

Warning: Forgetting break is the single most common switch bug — it causes every case after the matched one to run too, silently.

Strict comparison only. switch ("1") will not match case 1 — the string "1" and the number 1 are not === equal, unlike the loose comparisons a chain of if statements might accidentally allow.

default doesn't have to be last. It can appear anywhere in the switch and still only runs when no case matched, though placing it last is the near-universal convention for readability.

06 Mental Model

"A railway switchboard routing a train onto a track — and the train keeps rolling past the next signal unless something stops it."

The value picks which track to enter; break is the signal that stops the train there instead of letting it roll into whatever track comes next.

07 Interview Questions

Q: Is switch faster than an if/else chain?

Engines can sometimes optimize a switch into a jump table, but the honest reason to prefer it is readability for many discrete values on one variable, not guaranteed performance.

Q: Does switch use == or ===?

Strict equality (===) — per the ECMAScript spec's Strict Equality Comparison algorithm, with no type coercion between the switch expression and case values.

Key Takeaway

switch matches one value against several case labels with strict equality, and falls through to later cases unless a break stops it — a shape best suited to comparing a single variable against many discrete values.

Related Concepts