JavaScript Switch
A switch statement compares one value against a list of possible cases and runs the code for the matching one. It's a tidy alternative to a long chain of if…else if when every branch tests the same variable for equality.
Syntax
const day = 3;
let name;
switch (day) {
case 1:
name = "Monday";
break;
case 2:
name = "Tuesday";
break;
case 3:
name = "Wednesday";
break;
default:
name = "Some other day";
}
console.log(name);Wednesday
How it works:
- The expression in
switch (…)is evaluated once. - Its value is compared with each
caseusing strict equality (===). - Execution starts at the first matching case and continues until a
break(or the end of the switch). - If no case matches, the
defaultblock runs (if there is one).
Don't forget break
Without break, execution "falls through" into the next case — even though its value doesn't match:
const level = 2;
switch (level) {
case 1:
console.log("level 1");
case 2:
console.log("level 2");
case 3:
console.log("level 3");
default:
console.log("default");
}level 2
level 3
default
This is usually a bug. Occasionally, though, fall-through is useful on purpose.
Grouping cases
Stack several case labels to share one block of code:
function typeOfDay(day) {
switch (day.toLowerCase()) {
case "saturday":
case "sunday":
return "weekend";
case "monday":
case "tuesday":
case "wednesday":
case "thursday":
case "friday":
return "weekday";
default:
return "not a day";
}
}
console.log(typeOfDay("Sunday"), typeOfDay("FRIDAY"), typeOfDay("Funday"));weekend weekday not a day
Inside a function, return leaves the switch (and the function) immediately, so no break is needed.
Strict comparison
Because switch uses ===, types must match. A string "1" will not match case 1:
const input = "1"; // e.g. read from a form field
switch (input) {
case 1:
console.log("number one");
break;
case "1":
console.log("string one");
break;
}
switch (Number(input)) {
case 1:
console.log("converted first, now it matches");
break;
}string one
converted first, now it matches
Ranges with switch (true)
switch only checks equality, but a common trick is switch (true) with conditions as cases — the first case that evaluates to true wins:
const bmi = 23.4;
switch (true) {
case bmi < 18.5:
console.log("underweight");
break;
case bmi < 25:
console.log("normal");
break;
default:
console.log("overweight");
}normal
For ranges, an if…else if chain is usually clearer — use whichever reads better.
Alternative: lookup objects
When each case just maps a value to another value, an object (or Map) is often shorter than a switch:
const statusText = { 200: "OK", 404: "Not Found", 500: "Server Error" };
const text = statusText[code] ?? "Unknown";Block scope inside cases
All cases share one block, so declaring the same let/const name in two cases is an error. Wrap a case in braces to give it its own scope:
switch (action) {
case "add": {
const message = "Added";
break;
}
case "remove": {
const message = "Removed"; // fine — different block
break;
}
}Test yourself
What comparison does switch use to match cases?
- ==
- ===
- Object.is
- <
What happens when a matching case has no break?
- A SyntaxError
- The switch ends anyway
- Execution falls through into the next case
- The default block runs instead
When does the default block run?
- Always, at the end
- When no case matches
- Only when it is written first
- When a case has no break