Switch Statements
in JavaScript
Master the switch statement to write cleaner multi-branch logic and understand fall-through behavior.
What Is a Switch Statement?
A switch statement evaluates a single expression once, then compares its result against a list of possible case values. When a match is found, the code block for that case runs.
It is an alternative to long chains of if...else if...else when you are comparing the same variable against many discrete values.
Anatomy of the Switch
case value1: // code to run break; case value2: // code to run break; default: // fallback code }
Why Use Switch Over If-Else?
How the Switch Flows
Switch Uses Strict Equality (===)
Each case comparison uses ===, which checks both value and type. This means the number 1 does not match the string "1".
switch (x) {
case 1: console.log("number"); break; case "1": console.log("string"); // ✓ This runs break; }
Switch vs If-Else
| switch | if-else | |
|---|---|---|
| Comparison type | Strict equality (===) | Any boolean expression |
| Best for | Single variable, many values | Ranges, complex conditions |
| Fall-through | Yes (intentional) | No |
| Readability at 5+ branches | High | Low |
| Can test ranges (e.g., \( x \gt 10 \))? | Not directly | Yes |
Understanding Fall-Through
When a matching case is found and there is no break, execution "falls through" to the next case's code, regardless of whether that case matches.
Intentional Fall-Through
Fall-through is useful when multiple cases share the same action:
switch (day) {
case "Monday": case "Tuesday": case "Wednesday": case "Thursday": case "Friday": console.log("Weekday"); break; case "Saturday": case "Sunday": console.log("Weekend"); // ✓ This runs break; }
The default Clause
The default clause acts as a catch-all. It runs when no case value matches the expression. It is similar to the final else in an if-else chain.
switch (fruit) {
case "Apple": console.log("$1.50"); break; case "Banana": console.log("$0.75"); break; default: console.log("Price unknown"); // ✓ Runs }
How to Write a Switch
Example 1: Grade Converter
let message;
switch (grade) {
case "A": message = "Excellent"; break; case "B": message = "Good"; // ✓ Match! break; case "C": message = "Fair"; break; default: message = "Unknown grade"; }
console.log(message); // "Good"
Example 2: Season Finder
let season;
switch (month) {
case 12: case 1: case 2: season = "Winter"; break; case 3: case 4: case 5: season = "Spring"; // ✓ Match! break; case 6: case 7: case 8: season = "Summer"; break; case 9: case 10: case 11: season = "Autumn"; break; default: season = "Invalid month"; }
Example 3: switch(true) Pattern
You can pass true as the expression and use boolean conditions in each case to mimic range checks:
switch (true) {
case (score >= 90): console.log("A"); break; case (score >= 80): console.log("B"); // ✓ Runs break; case (score >= 70): console.log("C"); break; default: console.log("F"); }
Common Mistakes
Question 1 of 5
What type of comparison does a JavaScript switch statement use to match case values?
Question 2 of 5
Code:
switch (color) {
case "red": console.log("Red"); case "blue": console.log("Blue"); case "green": console.log("Green"); break; default: console.log("Other"); }
What is logged to the console?
Question 3 of 5
Which keyword is used to define the catch-all branch in a switch statement?
Question 4 of 5
Code:
switch (x) {
case "5": console.log("string five"); break; case 5: console.log("number five"); break; default: console.log("no match"); }
What is logged to the console?
Question 5 of 5
When is intentional fall-through most useful in a switch statement?