What is a Switch Statement?
A switch statement is a way to check a value against multiple options. Instead of writing many if/else statements, you use switch to make your code cleaner and easier to read
Think of it like:
- A TV remote with numbered buttons (1 = CNN, 2 = ESPN, 3 = HBO)
- A vending machine (A1 = Chips, B2 = Soda, C3 = Candy)
- A restaurant menu (selecting from options)
Switch Statement Syntax
javascript
switch (value) {
case option1:
// Code if value === option1
break;
case option2:
// Code if value === option2
break;
case option3:
// Code if value === option3
break;
default:
// Code if no match
}
Important parts:
switch – the keyword that starts the switch statement
case – each possible value to check
break – stops checking more cases
default – runs if nothing matches (optional)
Example 1: Day of the Week
javascript
let 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("Invalid day");
}
Example 2: Traffic Light
javascript
let light = "red";
switch (light) {
case "red":
console.log("Stop!");
break;
case "yellow":
console.log("Slow down");
break;
case "green":
console.log("Go");
break;
default:
console.log("Light is broken");
}
// Output: Stop!
Multiple Cases and Fall-Through
Sometimes multiple cases should do the same thing. You can group them together!
Example 1: Weekend vs Weekday
javascript
let day = "Saturday";
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
console.log("It's a weekday - time to work!");
break;
case "Saturday":
case "Sunday":
console.log("It's the weekend - relax!");
break;
default:
console.log("Invalid day");
}
// Output: It's the weekend - relax!
How it works:
- Multiple cases share the same code
- Only need one
breakat the end - Like saying “if any of these, do this”
Understanding Fall-Through Behavior
Fall-through happens when you forget the break statement. The code keeps running into the next case!
Example: Fall-Through (Without Break)
javascript
let number = 2;
switch (number) {
case 1:
console.log("One");
// No break! Falls through to case 2
case 2:
console.log("Two");
// No break! Falls through to case 3
case 3:
console.log("Three");
break;
default:
console.log("Other");
}
// Output:
// Two
// Three
What happened:
- Matched
case 2 - Printed
Two
- No
break, so continued tocase 3 - Printed
Three
- Hit
breakand stopped execution
When Fall-Through is Useful
Sometimes you WANT fall-through behavior:
Example :
javascript
Sometimes you WANT fall-through behavior:
let level = 3;
let permissions = [];
switch (level) {
case 3:
permissions.push("delete");
// Fall through - level 3 gets all permissions
case 2:
permissions.push("edit");
// Fall through - level 2 gets edit and view
case 1:
permissions.push("view");
break;
default:
console.log("No permissions");
}
console.log("Permissions: " + permissions);
// Output: Permissions: delete,edit,view
Fall-Through Warning Example
javascript
// ❌ WRONG - Forgot break statements
let choice = 1;
switch (choice) {
case 1:
console.log("Starting game...");
// Missing break!
case 2:
console.log("Loading game...");
// Missing break!
case 3:
console.log("Exiting game...");
break;
}
// Output (WRONG):
// Starting game...
// Loading game...
// Exiting game...
✅ CORRECT
javascript
// ✅ CORRECT - With break statements
switch (choice) {
case 1:
console.log("Starting game...");
break; // Stops here
case 2:
console.log("Loading game...");
break;
case 3:
console.log("Exiting game...");
break;
}
// Output (CORRECT):
// Starting game...
Practice Problems for Beginners
Practice 1: Month Number to Name
javascript
Convert month numbers (1-12) to month names.
function getMonthName(monthNumber) {
switch (monthNumber) {
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return "Invalid month number";
}
}
console.log(getMonthName(3)); // March
console.log(getMonthName(12)); // December
console.log(getMonthName(13)); // Invalid month numbe