Match One Value Against Many
JavaScript Switch
`switch` picks a branch based on a value. Cleaner than a long `if/else if` chain when you have many discrete cases.
What you'll learn
- Match a value with `case`
- Always include `break` (and know what happens when you don't)
- Provide a `default`
- Recognize when fall-through is intentional
switch lets you compare a value against many possibilities, one
case at a time. It’s especially handy when you have many discrete
options.
const day = 3;
switch (day) {
case 1: console.log("Monday"); break;
case 2: console.log("Tuesday"); break;
case 3: console.log("Wednesday"); break;
case 4: console.log("Thursday"); break;
case 5: console.log("Friday"); break;
case 6: console.log("Saturday"); break;
case 7: console.log("Sunday"); break;
default: console.log("Not a day");
}
// "Wednesday" Each case Needs a break
switch falls through to the next case unless you stop it with
break. Forgetting it is one of the classic switch bugs.
const fruit = "apple";
switch (fruit) {
case "apple":
console.log("It's an apple.");
// no break!
case "banana":
console.log("It's a banana.");
break;
case "grape":
console.log("It's a grape.");
break;
}
// "It's an apple."
// "It's a banana." ← oops, fell through Intentional Fall-Through
Sometimes fall-through is what you want — to handle multiple values the same way:
const day = 6;
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
console.log("Weekday");
break;
case 6:
case 7:
console.log("Weekend");
break;
default:
console.log("Not a day");
}
// "Weekend" default — the fallback
default runs when no case matches. It’s optional but recommended
— if you don’t have one and nothing matches, the switch does
nothing.
const role = "ghost";
switch (role) {
case "admin": console.log("Full access"); break;
case "user": console.log("Limited access"); break;
case "guest": console.log("Read only"); break;
default: console.log("Unknown role: " + role);
}
// "Unknown role: ghost" switch Uses Strict Equality
switch compares with ===. So case "5" won’t match the number
5.
const value = 5;
switch (value) {
case "5":
console.log("string five");
break;
case 5:
console.log("number five");
break;
}
// "number five" if/else if or switch?
Both work — pick by clarity:
- Use
switchwhen you’re testing one value against many specific possibilities. Cleaner and faster to scan. - Use
if/else ifwhen conditions are range checks, multiple variables, or complex expressions.
For mapping a value to a result, you might also reach for a plain object lookup:
const days = {
1: "Monday", 2: "Tuesday", 3: "Wednesday",
4: "Thursday", 5: "Friday", 6: "Saturday", 7: "Sunday",
};
const day = 3;
console.log(days[day] ?? "Not a day");
// "Wednesday" Often the cleanest of all.
Try It Yourself
Exercise
Number to weekday
const day = 3;
let name;
// your code here
💡 Show hint
✅ Show solution
const day = 3;
let name;
switch (day) {
case 1: name = "Monday"; break;
case 2: name = "Tuesday"; break;
case 3: name = "Wednesday"; break;
case 4: name = "Thursday"; break;
case 5: name = "Friday"; break;
case 6: name = "Saturday"; break;
case 7: name = "Sunday"; break;
default: name = "Not a day";
}
console.log(name);
Up Next
The logical operators that combine conditions.
JavaScript Logical Operators →