Skip to content

case

In JavaScript, the case keyword starts a clause inside a switch statement and gives a value to compare against the switch expression. When the switch statement runs, it evaluates the case expressions in source order and jumps to the first one that’s strictly equal (===) to its value. If none matches, it jumps to the default clause, if there is one.

Because matching is strict, the string "1" doesn’t match case 1:, and case NaN: never matches. Each case expression is evaluated only if no earlier clause matched.

A case clause has no end of its own. After a match, execution falls through into the following clauses until a break, return, or throw leaves the statement. All clauses also share one block scope, so a let or const name declared in one case clashes with the same name in another. Wrapping the clause’s statements in braces gives it its own block.

JavaScript case Keyword Syntax

Each case clause sits inside the body of a switch statement:

Language: JavaScript Syntax
switch (<expression>) {
  case <value1>:
    [<statements>]
  [case <value2>:
    [<statements>]]
  [default:
    [<statements>]]
}

JavaScript case Keyword Examples

A switch statement compares its value with each case label using strict equality:

Language: JavaScript
> function describe(value) {
…   switch (value) {
…     case 1:
…       return "the number 1";
…     case "a":
…     case "b":
…       return "an early letter";
…     default:
…       return "something else";
…   }
… }
undefined

> describe(1)
'the number 1'
> describe("1")
'something else'

> describe("b")
'an early letter'

The string "1" reaches default because it isn’t strictly equal to 1. Stacking the "a" and "b" labels with no statements between them gives both values one body.

Without a break, a matching clause falls through into the next one:

Language: JavaScript
> switch (2) {
…   case 1:
…     console.log("one");
…   case 2:
…     console.log("two");
…   case 3:
…     console.log("three");
…     break;
…   case 4:
…     console.log("four");
… }
two
three
undefined

Execution starts at case 2:, runs into case 3:, and stops at its break.

JavaScript case Keyword Use Cases

  • Mapping a value to one of several fixed actions or results
  • Grouping several values that share the same handling
  • Replacing a chain of if and else comparisons against one value
  • Handling each variant of a string or number tag in a state machine or reducer

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Nov. 4, 2026