You will be able to use a JavaScript switch statement to match one value against several cases and choose the correct branch cleanly.
What it is
A switch statement compares one expression against many possible values. It is like a multi-way branch: JavaScript evaluates the expression, then looks for the first case whose value matches using strict equality (===). If no case matches, it runs the default block if present.
Related terms: case, default, fallthrough, and break. A case is one possible match. Fallthrough means execution continues into the next case when a case does not stop with break, return, or throw.
Why it matters
- It makes one-variable, many-value logic easier to read than a long chain of
ifstatements. - It groups several cases together, such as weekend days, without repeating code.
- It gives a clear place for unexpected input using
default. - It helps when values are discrete, such as status codes, menu choices, or day names.
Syntax or steps
- Write
switch (value)with the expression you want to test. - Add one or more
case possibleValue:lines. - Put the code for that case after the colon.
- End each case with
break;, or usereturn;if inside a function. - Add
default:for values that do not match any case.
Example
function schedule(day) {
switch (day) {
case "Sat":
case "Sun":
return "rest";
case "Mon":
case "Tue":
case "Wed":
case "Thu":
case "Fri":
return "work";
default:
return "unknown";
}
}
console.log(schedule("Sat")); // "rest"
console.log(schedule("Mon")); // "work"
console.log(schedule("X")); // "unknown"
The function receives one value, day. The first two cases share the same result: if day is "Sat" or "Sun", the function returns "rest". The next five cases return "work". If the value is not one of the listed strings, default returns "unknown". Because each branch uses return, no break is needed.
Common mistakes
- Forgetting
breakorreturn. The code may fall through to the next case. Fix it by ending each case intentionally. - Expecting loose matching.
switchuses strict equality, so1does not match"1". Fix it by comparing the same type. - Declaring variables inside a case without braces. A
letorconstbelongs to the whole switch block and can cause errors. Fix it by wrapping the case body in{ }. - Leaving out
default. Unexpected values may do nothing. Fix it by adding a default branch for logging, errors, or fallback behavior.
When to use it
| Use | Best when | Example |
|---|---|---|
switch | One value is compared to many exact values. | status === "active", "pending", "closed" |
if / else if | Conditions involve ranges, multiple variables, or complex logic. | score >= 90 && level === "advanced" |
Use switch for clean discrete matching. Use if when the test is not just “does this value equal one of these values?”
Practice
Guided exercise: Write a function called messageForStatus(status) that returns "Ready" for "active", "Waiting" for "pending", and "Closed" for "closed". Use default to return "Unknown". Expected output: messageForStatus("pending") returns "Waiting".
Challenge: Write a switch that maps a month number from 1 to 12 to a season. Hint: group months with multiple case lines, such as 12, 1, and 2 for winter.
Quick check
What happens if a matching case does not end with break, return, or throw?
JavaScript continues running the next case’s code. This is called fallthrough.
Summary
A switch statement is a compact way to branch on one value against many exact cases. It works best when each case is a clear, discrete match and each branch stops intentionally.