All topics
Fundamentalsbeginner

Switch Statements and Fall-Through

How switch statements compare values with strict equality and why forgetting break causes cases to 'fall through.'

switch compares with strict equality and, without break, continues into the next case.

A switch statement without breaks is like walking into a building through the right door but then finding all the internal doors between rooms wedged open — you keep walking through every room after that, whether you meant to or not, until you hit an actual closed door (break).

Key Concepts

1
    switch (status) {
      case 'OPEN':
        handleOpen();
        break;
      case 'CLOSED':
        handleClosed();
        break;
      default:
        handleUnknown();
    }
2
Strict equality is the part people forget.
    switch (1) {
      case '1': console.log('string');  // not reached
      case 1:   console.log('number');  // reached
    }
3
No coercion. A numeric id compared against string cases matches nothing and falls to default, which is a common bug with values that came from a URL or a form.
default
4
Fall-through is usually a bug and occasionally the point.
    switch (day) {
      case 'SAT':
      case 'SUN':
        return 'weekend';    // deliberate: both cases share it
      default:
        return 'weekday';
    }
5
Grouping cases like this is the legitimate use. Anything else that falls through is nearly always a missing break, and it produces behaviour that looks random — two handlers running for one input.
break
6
switch(true) handles ranges.
    switch (true) {
      case score >= 90: return 'A';
      case score >= 80: return 'B';
      default:          return 'F';
    }
switch(true)
7
Each case is evaluated as a boolean and the first true one wins. Order matters, exactly as in an if chain — put the narrowest condition first or it never runs.
if
8
Block scoping inside cases. Cases share one scope, so a let in one case collides with the same name in another:
let
9
    case 'a': let x = 1; break;
    case 'b': let x = 2; break;   // SyntaxError
10
Braces around each case body fix it.
11
When to use an object instead.
    const handlers = { OPEN: handleOpen, CLOSED: handleClosed };
    (handlers[status] ?? handleUnknown)();
12
For a simple value-to-action mapping this is shorter, has no fall-through to get wrong, and can be built or extended at runtime. switch earns its place when the cases involve conditions rather than a lookup.
switch
13
What the interviewer is probing.1. "What comparison does switch use?" Probing: the operator. Stalls: "Loose equality." Moves up: strict equality, so "1" does not match case 1.
14
2. "When is fall-through useful rather than a bug?" Probing: intent. Stalls: "Never." *Moves up:* grouping several cases that share one body — worth a comment so the next reader knows it is deliberate.
15
3. "Why would you add braces to a case?" Probing: block scope. Stalls: "Style." Moves up: let and const in a case leak to the whole switch block without them, so two cases declaring the same name collide.
16
4. "When would you use an object lookup instead?" Probing: the alternative. Stalls: "Switch is always fine." Moves up: when every case maps a value to a result — a lookup object or Map is shorter and extensible without editing the function.