
JavaScript Tutorial – Day 6: Switch Case in JavaScript
When writing code, we often need to check multiple conditions. Using too many if-else statements makes the code look messy. That’s where the switch case in JavaScript comes in. In this JavaScript tutorial, we’ll learn how the switch case works, why it’s useful, and see real-world examples.
What is Switch Case in JavaScript?
The switch case in JavaScript is a control statement that lets you compare a value against multiple possible cases. Instead of writing long chains of if-else if
, you can use switch
for cleaner, more readable code.
Syntax of Switch Case in JavaScript
switch (expression) {
case value1:
// code to execute if expression === value1
break;
case value2:
// code to execute if expression === value2
break;
default:
// code to execute if no cases match
}
👉 Explanation:
expression
→ the variable or value you are testing.case
→ each possible match.break
→ stops execution after a match.default
→ runs if no case matches.
Example: Switch Case in 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");
}
👉 Output: Wednesday
Real-Life Example of Switch Case in JavaScript
Imagine you are building a restaurant ordering system where customer can order from food menu and you serve only that is ordered:
let order = "pizza";
switch (order) {
case "burger":
console.log("You ordered a Burger 🍔");
break;
case "pizza":
console.log("You ordered a Pizza 🍕");
break;
case "pasta":
console.log("You ordered Pasta 🍝");
break;
default:
console.log("Sorry, item not available.");
}
👉 Output: You ordered a Pizza 🍕
Nested Switch Case in JavaScript
You can also use one switch case inside another switch.
let subject = "Science";
let topic = "Physics";
switch (subject) {
case "Science":
switch (topic) {
case "Physics":
console.log("You chose Physics 🔭");
break;
case "Chemistry":
console.log("You chose Chemistry ⚗️");
break;
}
break;
default:
console.log("Unknown subject");
}
👉 Output: You chose Physics 🔭
When to Use Switch Case in JavaScript?
✅ Use switch case when:
- You have multiple possible values for one variable.
- You want cleaner, more readable code than long if-else chains.
❌ Avoid switch case when:
- Complex conditions (like
age > 18 && isMember
) are needed. - You need ranges (better handled with if-else).
Best Practices for Switch Case in JavaScript
- Always use
break
to prevent fall-through. - Use
default
to handle unexpected values. - Keep it simple; avoid deeply nested switches.
External Resource
👉 Read the official MDN Switch Statement Documentation for advanced usage.
Quick Recap
switch
is used for checking multiple values.case
defines possible matches.break
prevents fall-through.default
runs if no match is found.
What’s Next?
In this JavaScript tutorial series:
- ✅ Day 5 → If-Else Statements in JavaScript
- ✅ Day 6 → Switch Case in JavaScript
- 🔜 Day 7 → Loops in JavaScript (for, while, do-while)