JavaScript Switch
if / else shines when the question is yes/no or a range. Sometimes the question is different: which of several fixed values is this? A day code 1…7, a status string "ok" / "warn" / "err", a menu key. For that shape, switch is the clean tool. You already know branching; here you match one value to labeled cases.
What does switch do?
switch looks at one expression in parentheses. It compares that value to each case label. When a label matches, JavaScript runs the statements under that case. break ends the switch so the next cases are skipped. default is the fallback when no case matched.
The pieces at a glance
📊 Everyday switch pieces
| Keyword | Everyday reading | Typical use |
|---|---|---|
switch |
look at this value… | open the choice |
case |
if it equals this label… | one fixed option |
break |
stop here; leave the switch | end after a match |
default |
if nothing matched… | fallback path |
Matching uses a strict equality style check (same idea as ===). A number case will not match a string of digits. Keep the value and the labels in the same “shape.”
let day = 3;
switch (day) {
case 1:
console.log("Mon");
break;
case 2:
console.log("Tue");
break;
case 3:
console.log("Wed");
break;
default:
console.log("other");
}
One value, several labeled paths
Here day is 3, so the case 3 path runs and the Console prints Wed. Cases 1 and 2 were checked and skipped. After break, the switch is finished — default does not run.
Without break, JavaScript would keep running into the next case bodies. That is called fall-through. Beginners almost always want a break after each case they finish.
let day = 2;
switch (day) {
case 1:
console.log("Mon");
case 2:
console.log("Tue");
case 3:
console.log("Wed");
break;
default:
console.log("other");
}
Missing break runs later cases too
day is 2. The match starts at case 2, prints Tue, then continues into case 3 and prints Wed before break. You wanted one label; you got two. Put break under each finished case unless you deliberately share code.
let day = 9;
switch (day) {
case 1:
console.log("Mon");
break;
case 2:
console.log("Tue");
break;
default:
console.log("other");
}
default covers “none of these”
day is 9. No case matches, so default runs and you see other. Use default when every unexpected value still needs a safe message. You can omit it only when silence is acceptable.
let status = "warn";
switch (status) {
case "ok":
console.log("green");
break;
case "warn":
console.log("yellow");
break;
case "err":
console.log("red");
break;
default:
console.log("unknown");
}
Cases can be strings too
status is "warn", so you see yellow. String cases are common for short codes from a form or API. Ranges like “score from 60 to 90” still belong in if / else if — switch wants discrete labels.
let day = 1;
let label;
switch (day) {
case 1:
label = "Mon";
break;
case 2:
label = "Tue";
break;
default:
label = "other";
}
console.log(label);
Name the result of the match
Set label inside each case (and in default), then use it once afterward. Here label becomes "Mon". Same pattern you used with if / else — choose first, use later.
Show the chosen path on the page
Pick a short message with switch, then write it into the page.
<!DOCTYPE html>
<html>
<head>
<title>Switch — page choice</title>
</head>
<body>
<p id="out">…</p>
<script>
let day = 3;
let msg;
switch (day) {
case 1:
msg = "Mon";
break;
case 2:
msg = "Tue";
break;
case 3:
msg = "Wed";
break;
default:
msg = "other";
}
let box = document.getElementById("out");
box.textContent = "day=" + msg;
</script>
</body>
</html>
day is 3, so the page shows day=Wed. Change day to 9, refresh, and you should see day=other. The next lesson (for) repeats work many times; switch stays the tool for “which fixed label?”
Try it yourself
- Build a
switchonlet day = 1that logs"Mon"for case1. - Add cases for
2and3withbreakafter each. - Set
dayto9and confirmdefaultlogs"other". - Switch on a string
statuswith"ok","warn","err". - Temporarily remove one
breakand watch fall-through in the Console. - In the HTML demo, change only
day, refresh, and read the newday=text.
Common mistakes
- Forgetting
breakafter a case (later cases run too) - Using
switchfor open ranges (score >= 60) — preferif/else if - Mixing types so a number never matches a string case
- Omitting
defaultwhen unknown values still need a message - Replacing every
ifwithswitchwhen the check is not a fixed label list
Summary
switchmatches one value to severalcaselabelsbreakstops the switch after a matchdefaultruns when nothing matched- Best for discrete values; ranges stay with
if/else if - Store a label when you need the result later; show it on the page when useful
- Next: for loop — repeating work a set number of times
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top