JavaScript Switch Statement – Multiple Condition Handling
The switch statement is used to perform different actions based on different conditions. It’s a cleaner alternative to multiple if...else if
statements, especially when you have many conditions to check.
1. Basic Syntax of Switch:
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 expression doesn't match any case
}
expressionis the value you want to evaluate.case value:defines a value to compare against the expression.break;stops the execution of the switch statement and prevents it from running the following cases.default:defines code that runs if none of thecasevalues match the expression.
Example:
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");
}
Explanation:
- The expression
day = 3is compared with eachcase. Whendayequals 3,"Wednesday"is printed.
2. Switch Without Break:
If you omit the break statement, JavaScript will continue executing the following cases even if a match is found. This is called “falling through.”
Example:
let fruit = "apple";
switch (fruit) {
case "apple":
console.log("It's an apple");
case "banana":
console.log("It's a banana");
case "orange":
console.log("It's an orange");
default:
console.log("Unknown fruit");
}
Explanation:
- Since there’s no
break, JavaScript will “fall through” and print all the messages, even after the match for"apple"is found. - To prevent this, you should always use
breakunless you want fall-through behavior.
3. Switch with Multiple Cases:
You can group multiple cases together to execute the same code for different values.
Example:
let number = 2;
switch (number) {
case 1:
case 2:
case 3:
console.log("Number is between 1 and 3");
break;
default:
console.log("Number is not between 1 and 3");
}
Explanation:
- The code
"Number is between 1 and 3"will be printed because thenumbermatches with2(which is grouped with1and3).
4. Switch with Expressions:
You can use expressions in a switch statement instead of just constants.
Example:
let x = 5;
switch (true) {
case (x > 0 && x < 10):
console.log("x is between 0 and 10");
break;
case (x >= 10 && x < 20):
console.log("x is between 10 and 20");
break;
default:
console.log("x is out of range");
}
Explanation:
- The
switch(true)checks each condition and matches the first case where the condition evaluates totrue.
Conclusion:
- The
switchstatement provides an easier way to test multiple conditions compared to multipleif...elsestatements. - Use the
breakstatement to stop the execution after a matchingcaseis found. - Fall-through behavior occurs if you omit
breakafter acase. - The
switchstatement can also evaluate expressions, making it versatile for various conditions.