switch

Compares a value against a list of case labels and runs the code from the first match.

On this page

Example

#
JavaScript
function taskLabel(type) {
  switch (type) {
    case 'video':
      return 'Video'
    case 'question':
      return 'Quiz'
    case 'gap':
    case 'sort':
      return 'Exercise'
    default:
      return 'Lesson'
  }
}

console.log(taskLabel('video'))
console.log(taskLabel('sort'))
console.log(taskLabel('analyze'))
Console
Video
Exercise
Lesson

Definition and usage

#

The switch statement evaluates an expression once, then compares the result against the case labels one by one. When it finds a match, it runs the code from that point. If nothing matches, it falls through to the optional default.

The comparison uses the strict === operator, so the string "1" doesn’t match the number 1. Each case usually ends with break or return. Without it, execution “falls through” to the next case, which is sometimes a bug, but also lets you combine several cases into one.

switch is readable when you’re checking one variable against several specific values, like a task type or a command name. For numeric ranges and compound conditions, if...else fits better.

Syntax

#
Syntax
switch (expression) {
  case value1:
    // statements
    break
  case value2:
  case value3:
    // shared statements
    break
  default:
    // when nothing matches
}

More examples

#
Pitfall: a missing break
JavaScript
const level = 2

switch (level) {
  case 1:
    console.log('Unlocked: HTML')
  case 2:
    console.log('Unlocked: CSS')
  case 3:
    console.log('Unlocked: JavaScript')
    break
  default:
    console.log('No rewards')
}
Console
Unlocked: CSS
Unlocked: JavaScript

Execution starts at case 2 and keeps running until it hits break. Sometimes that’s intentional, but far more often it’s simply a forgotten break.

Strict comparison
JavaScript
const answer = '1'

switch (answer) {
  case 1:
    console.log('the number 1')
    break
  case '1':
    console.log('the string "1"')
    break
}
Console
the string "1"
switch (true) for ranges
JavaScript
const points = 75

switch (true) {
  case points >= 90:
    console.log('Gold medal')
    break
  case points >= 60:
    console.log('Silver medal')
    break
  default:
    console.log('Bronze medal')
}
Console
Silver medal

This pattern works because the result of each condition is compared against true. Many developers prefer a plain if...else in this situation.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • End every case with break or return. If falling through is intentional, leave a comment, e.g. // falls through, so no one mistakes it for a bug.
  • When declaring variables inside a case, wrap its code in braces: case "video": { ... }. All the cases share one block, so two consts with the same name cause a syntax error.
  • When switch just maps one value to another, an object is often simpler: const labels = { video: "Video", question: "Quiz" } and the lookup labels[type].

Related entries

#

Spotted a mistake or missing an example? Write to us.