How to Use the JavaScript Switch Case: Full Guide
The switch statement compares one value against multiple cases. Learn the syntax, how break and default work, and when switch is cleaner than if/else.
A switch statement compares one value against several possible cases and runs the matching code. It is an alternative to a long if/else if chain when you are checking the same variable against different exact values.
const fruit = "apple";
switch (fruit) {
case "banana": console.log("Yellow and curved."); break;
case "apple": console.log("Red or green, crisp."); break;
case "orange": console.log("Citrus and round."); break;
default: console.log("Unknown fruit.");
}
// "Red or green, crisp." prints because fruit is "apple"This is the switch pattern: one value compared against multiple cases, with a break after each and a default at the end.
The switch Syntax
Stripped down to its skeleton, a switch statement is just a value, a list of possible matches, and an optional fallback. Here is the general shape without any real logic filled in:
switch (expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code when nothing matches
}Every switch statement has four parts:
- switch (expression): the value to compare. This is evaluated once at the start.
- case value:: a possible match. JavaScript compares the expression against this value using strict equality (
===). - break: exits the switch. Without it, execution "falls through" to the next case.
- default: runs when no case matches. It is optional but recommended, like else at the end of an if chain.
How switch Evaluates
JavaScript checks each case from top to bottom. The first match that is strictly equal to the expression runs. After the matching code executes, break exits the entire switch. If no case matches, default runs.
The break Keyword
break stops execution and jumps out of the switch. Without it, JavaScript keeps running the next case's code even if that case does not match:
const day = "Monday";
switch (day) {
case "Monday":
console.log("Start of week.");
// Missing break!
case "Tuesday": console.log("Second day."); break;
default: console.log("Other day.");
}
// "Start of week."
// "Second day." -- also prints because there is no break after MondayThe missing break after "Monday" causes fall-through: both Monday's and Tuesday's code run. This is almost always a bug. Always add break after each case.
Intentional Fall-Through
Sometimes fall-through is useful. When multiple cases should run the same code, stack them without a break:
const day = "Saturday";
switch (day) {
case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday":
console.log("Weekday. Time to work.");
break;
case "Saturday": case "Sunday":
console.log("Weekend. Time to relax.");
break;
}
// "Weekend. Time to relax."The five weekday cases all fall through to the same log statement. The two weekend cases do the same. This is cleaner than repeating the same code six times or writing a complex condition.
The default Case
default runs when no case matches. Think of it as the else at the end of an if/else chain:
const status = "unknown";
switch (status) {
case "active": console.log("User is active."); break;
case "inactive": console.log("User is inactive."); break;
default: console.log("Unknown status:", status);
}
// "Unknown status: unknown"Always include a default case, even if you think every possible value is covered. It catches unexpected values and makes debugging easier. You can place default anywhere in the switch, but putting it last is the clearest convention.
switch Uses Strict Equality
switch compares using strict equality (===), so the type must match:
const value = "3";
switch (value) {
case 3: console.log("Number 3"); break;
case "3": console.log("String 3"); break;
}
// "String 3" -- "3" === "3" is true, "3" === 3 is falseThis is different from if with loose equality, which would match both. For more on this distinction, see the comparison operators guide.
When switch Shines
Use switch when you are comparing one variable against many specific values. This if/else if version repeats the same variable name in every branch:
// if/else if -- repetitive variable name
if (direction === "up") { moveUp(); }
else if (direction === "down") { moveDown(); }
else if (direction === "left") { moveLeft(); }
else if (direction === "right") { moveRight(); }
else { console.log("Unknown direction"); }The equivalent switch statement writes direction once and lists every value it might match, instead of repeating the same comparison in front of each branch:
// switch -- variable name appears once
switch (direction) {
case "up": moveUp(); break;
case "down": moveDown(); break;
case "left": moveLeft(); break;
case "right": moveRight(); break;
default: console.log("Unknown direction");
}The switch version reads as "check direction against up, down, left, right." The if/else version repeats the direction comparison four times. When every condition is a simple equality check against the same variable, switch is usually cleaner. For a deeper comparison, see the switch vs if else guide.
Rune AI
Key Insights
- switch compares one expression against multiple case values using strict equality (===).
- Each case should end with break to prevent unintentional fall-through.
- The default case runs when no other case matches, like else in an if chain.
- Fall-through can be intentional: empty cases share the next case's code.
- Use switch for exact value matches; use if/else if for ranges and complex conditions.
- switch works with any data type: strings, numbers, booleans, and more.
Frequently Asked Questions
What happens if I forget break in a switch case?
Can I use ranges or conditions inside a case?
Does switch work with strings or only numbers?
Conclusion
The switch statement is a clean way to compare one value against many specific cases. Use it when you find yourself writing the same variable name repeatedly in an if/else if chain. Remember to add break after each case unless you want intentional fall-through, always include a default case as a safety net, and know that switch uses strict equality (===) for comparisons.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.