JavaScript Switch Case
JavaScript Switch Case
The switch statement evaluates an expression and executes the matching case block. The switch statement provides a cleaner way to handle multiple conditional checks based on specific values.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression === value1
break;
case value2:
// Code to execute if expression === value2
break;
// Add more cases as needed
default:
// Code to execute if none of the cases match
}
How it Works:
- The
expressionis evaluated once. - Each
casevalue is matched against theexpressionto determine execution. - When a match occurs, the corresponding case block runs.
- The
breakstatement stops execution from continuing to the next case. - The
defaultcase (optional) runs if no other case matches.
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;
default:
console.log("Weekend");
}
Explanation:
- The value of
dayis3. -
The
switchstatement comparesdayto eachcase:- It does not match
1or2. - It matches
3, so"Wednesday"is printed.
- It does not match
- The
breakprevents execution from continuing to the next cases.
Output:
Wednesday
Example with No break:
Without a break, execution moves sequentially through subsequent cases until one is terminated or the switch block ends.
function categorizeNumber(num) {
let result = "";
switch (num) {
case 1:
result += "One ";
case 2:
result += "Two ";
case 3:
result += "Three ";
default:
result += "Number or higher";
}
return result.trim();
}
console.log(categorizeNumber(1)); // Output: "One Two Three Number or higher"
console.log(categorizeNumber(2)); // Output: "Two Three Number or higher"
console.log(categorizeNumber(3)); // Output: "Three Number or higher"
console.log(categorizeNumber(4)); // Output: "Number or higher"
Explanation::
- No
breakstatements: Once the code enters acase, all subsequent cases (includingdefault) are executed, regardless of whether they match or not. -
Result:
- For
num = 1, it starts at case1and continues through all cases anddefault. - For
num = 2, it starts at case2and continues through the remaining cases. - For
num = 4, it skips all cases and only executesdefault.
- For
To avoid this "fall-through" behavior, always use break unless it's intentional.
Example with default:
In JavaScript, a switch statement is used to perform different actions based on different conditions. The default case runs when no matching case value is found in the switch statement. It acts as a fallback option.
Key Points:
- The
breakstatement stops further case evaluations within aswitchblock. - When no matching
caseis found, thedefaultblock executes if defined.
Example:
Here's an example that determines the type of a day based on a numeric input:
const dayNumber = 5;
switch (dayNumber) {
case 1:
console.log("Monday - Start of the workweek!");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday - Midweek");
break;
case 4:
console.log("Thursday");
break;
case 5:
console.log("Friday - Almost weekend!");
break;
case 6:
console.log("Saturday - Weekend!");
break;
case 7:
console.log("Sunday - Relax and recharge!");
break;
default:
console.log("Invalid day number. Please enter a number between 1 and 7.");
}
Explanation:
dayNumberis the input expression being evaluated.- If
dayNumbermatches1,2, etc., the correspondingcaseblock runs, and thebreakensures no further cases execute. - If
dayNumberdoesn't match anycase, thedefaultblock executes to handle invalid inputs.
Output:
If dayNumber is 5, the output will be:
Friday - Almost weekend!
If dayNumber is 8, the output will be:
Invalid day number. Please enter a number between 1 and 7.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand Javascript Tutorial visually:
What You'll Learn:
- 📌 Learn Switch Statements In 7 Minutes
- 📌 Learn JavaScript SWITCHES in 6 minutes