JavaScript switch Statement
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
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.
Desktop





Tablets & Mobile





