Favorites

JavaScript switch Statement

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
switch
JavaScript Control FlowThis statement is supported by all modern browsers.
Updated 28th November 2026

The switch statement compares a value against a list of cases and runs the code for the first one that matches, with an optional default.

Description

The switch statement evaluates an expression and compares its value with each case using strict equality (===). Execution starts at the first matching case and continues until a break (or return); without one, it falls through into the next case. The optional default clause runs when nothing matches.

Fall-through can be useful for grouping cases that share code, but forgetting a break is a common bug. Variables declared with let or const in different cases share one scope, so wrap a case's code in braces if it declares variables.

A switch works best when you compare one value against several fixed options, such as a status code or a command name. For simple mappings from a key to a value, an object or Map lookup is often shorter, for example const label = labels[status] ?? 'Unknown'. To match ranges or more complex conditions, a series of if...else if statements is clearer than the switch (true) trick.

Syntax

switch (expression) {
  case value1:
    statements
    break;
  case value2:
    statements
    break;
  default:
    statements
}

Example

function dayType(day) {
  switch (day) {
    case 'Sat':
    case 'Sun':
      return 'Weekend';
    case 'Fri':
      return 'Almost weekend';
    default:
      return 'Weekday';
  }
}
console.log(dayType('Sun'), dayType('Fri'), dayType('Tue'));

Browser Support

The following information will show you the current browser support for the switch statement. Hover over a browser icon to see the version that first introduced support for this statement.

This statement is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee