switch Statements

JavaScript ES2024 · ✓ verified by execution on 2026-08-07

A switch statement evaluates an expression and matches the result against a series of case clauses. It’s often used as a cleaner alternative to a long chain of if...else if statements when checking a single variable against multiple specific values.

The Basic Syntax

The switch statement executes statements after the first case clause with a matching value, until a break statement is encountered.

javascript
// Basic switch statement executing a matched case block.
const animal = 'Dog';
switch (animal) {
  case 'Cat':
    console.log('Meow');
    break;
  case 'Dog':
    console.log('Woof');
    break;
  default:
    console.log('Unknown');
}
Output
Woof

The Default Clause

If none of the case clauses match, the switch statement jumps to the default clause. Think of it like the final else in an if/else chain.

javascript
// Default block acts as a fallback when no cases match.
const val = 'Fish';
switch (val) {
  case 'Bird':
    console.log('Chirp');
    break;
  default:
    console.log('Unknown animal');
    break;
}
Output
Unknown animal

Strict Equality

A very important detail: switch statements use strict equality (===) when matching. They do not perform type coercion.

javascript
// Switch statements use strict equality (===), meaning '1' does not match 1.
const number = 1;
switch (number) {
  case '1':
    console.log('String 1');
    break;
  case 1:
    console.log('Number 1');
    break;
}
Output
Number 1

Fall-Through Behavior

If you omit the break statement at the end of a case block, JavaScript will continue executing the code in the next case clauses, regardless of whether their values match. This is called “falling through”.

Let’s visualize how the execution falls through to subsequent lines:

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

// Execution falls through to the next case if break is omitted.
let count = 0;
switch (1) {
  case 1:
    count += 1;
  case 2:
    count += 10;
    break;
  case 3:
    count += 100;
}
console.log(count);
Output
11

The Position of Default

While it’s common practice to place the default clause at the very end, it can technically be placed anywhere.

javascript
// The default block can be placed anywhere, not just at the end.
const val = 'x';
switch (val) {
  default:
    console.log('Default block');
    break;
  case 'y':
    console.log('Y block');
}
Output
Default block

Grouping Cases

You can intentionally use the fall-through behavior to stack multiple cases that should run the exact same code.

javascript · visualize
// Multiple cases can be stacked to share the same execution block.
const grade = 'B';
switch (grade) {
  case 'A':
  case 'B':
  case 'C':
    console.log('Pass');
    break;
  case 'D':
  case 'F':
    console.log('Fail');
    break;
}
Pass

Why Switch Statements Exist

The switch statement exists as a cleaner, more readable alternative to long chains of if/else if blocks when comparing a single expression against multiple constant values. Under the hood, many languages optimize switch statements using jump tables, making them potentially faster than sequential if evaluations, although this depends heavily on the specific JavaScript engine.

A case is not a block. Inherited from C, switch treats each case as a label: once one matches, execution continues straight through every line below it, into the next case and the next, until it meets a break or returns. Forget the break and several cases run in sequence — which is occasionally what you want, and usually a bug.

switch(val) case 'A': (Match!) case 'B': (Executes!) falls through
Switch fall-through mechanism visualization

Check yourself

What type of comparison does a switch statement use to match cases?

Reveal answer

Strict equality (===) — Switch statements use strict equality (===), so the types must match exactly.

What happens if you omit a break statement at the end of a case block?

Reveal answer

Execution falls through to the next case — Execution continues into subsequent case clauses regardless of whether their values match, which is called fall-through.

Is the default clause required in a switch statement?

Reveal answer

No, it is completely optional — The default clause acts as a fallback and is entirely optional.

Challenges

Challenge 1 +50 XP

Write a switch statement that logs 'Stop' if color is 'Red', and 'Go' if color is 'Green'.

javascript
  • Test 1 — expects "Stop"
Show solution (0 XP)
const color = 'Red';
switch (color) {
  case 'Red':
    console.log('Stop');
    break;
  case 'Green':
    console.log('Go');
    break;
}

Challenge 2 +50 XP

Write a switch statement with a default clause that logs 'Other' if the value is not 'A' or 'B'.

javascript
  • Test 1 — expects "Other"
Show solution (0 XP)
const letter = 'C';
switch (letter) {
  case 'A':
    console.log('A');
    break;
  case 'B':
    console.log('B');
    break;
  default:
    console.log('Other');
    break;
}