What Is the JavaScript switch Statement?
The JavaScript switch statement is used to execute different blocks of code based on a value.
It is useful when you need to compare one variable with multiple possible values. Instead of writing multiple if...else if conditions, you can use a switch statement to make your code cleaner and easier to read.
Syntax
switch (expression) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code if no case matches
}
Keywords Explained
- switch: Starts the switch statement.
- case: Defines a value to compare.
- break: Stops execution of the switch statement.
- default: Executes when no case matches.
Example 1: Simple switch Statement
Let’s check the day of the week using a switch statement.
let day = 3;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Invalid day");
}
Output:
Wednesday
In this example, the value of day is 3. JavaScript matches it with case 3 and prints Wednesday.
Example 2: Using the default Case
The default case executes when none of the cases match.
let role = "customer";
switch (role) {
case "admin":
console.log("Welcome, Admin");
break;
case "seller":
console.log("Welcome, Seller");
break;
default:
console.log("Welcome, Customer");
}
Output:
Welcome, Customer
Here, no case matches "customer", so the default block executes.
Example 3: Multiple Cases with the Same Code
You can use multiple cases to execute the same block of code.
let day = "Saturday";
switch (day) {
case "Saturday":
case "Sunday":
console.log("Weekend");
break;
default:
console.log("Weekday");
}
Output:
Weekend
Both Saturday and Sunday execute the same code.
Why Is break Important?
The break statement stops execution after a matching case.
Without break, JavaScript continues executing the following cases. This behavior is called fall-through.
let number = 1;
switch (number) {
case 1:
console.log("One");
case 2:
console.log("Two");
}
Output:
One
Two
Use break when you want to execute only the matching case.
switch vs if…else
| switch | if…else |
|---|---|
| Compares one expression against multiple values. | Checks different conditions. |
| Useful for fixed values. | Useful for ranges and complex conditions. |
| Uses strict equality for case matching. | Supports operators such as >, <, and &&. |
Use switch for fixed options such as order status or user roles. Use if...else when checking conditions such as whether a price is greater than ₹1,000.
Common Mistakes
- Forgetting break: This can cause unwanted execution of subsequent cases.
- Using different data types: The number
1does not match the string"1". - Ignoring default: Use a default case when you need to handle unexpected values.
- Using switch for complex conditions: In such cases,
if...elsemay be easier to understand.
Frequently Asked Questions
1. What is the use of switch in JavaScript?
The switch statement executes different code blocks based on the value of an expression.
2. Is break mandatory in switch?
No. However, without break, execution may continue into subsequent cases.
3. Can we use strings in switch?
Yes. A switch statement can compare strings, numbers, and other values.
4. Can a switch statement have multiple default cases?
No. A switch statement can contain only one default label.
5. Does switch use strict equality?
Yes. JavaScript uses strict equality (===) to match case values.
Conclusion
The JavaScript switch statement is useful for handling multiple fixed values in a clean and readable way. Understanding case, break, and default will help you write better JavaScript code.