Using switch for multiple conditions
In JavaScript programming, switch statement is a better option to apply many small if-else if-else block flow control statements in default statement. When JavaScript programmer needs to Analyze many possible conditions based on an expression value in a single program. Then switch statement is useful in a specific particular condition in JavaScript program, when JavaScript programmer wants many options to check many multiple conditions or expressions in a single program, switch statement makes your program simple and effective.

Syntax of switch statement in JavaScript.
switch (condition/expression) {
case value1:
// once the code when executed if expression === value1
break;
case value2:
/ once the code when executed if expression === value2
break;
case value3:
/ once the code when executed if expression === value3
break;
case value4:
/ once the code when executed if expression === value4
break;
default:
/ here code to be executed only if none of the above cases expression condition match properly
}
Detail about Switch statement.
- condition expression – This is the value or variable in the current program that the programmer is going to analyse in the JavaScript program.
- case values - These are the different case value information in the switch statement that your program can match the expression against at run time.
- break statement – This breaks the source code in the current switch case statement from moving to the next case expression value. Without a break in the switch case statement, the program will continue to execute the case expression even after a match is found until the proper case condition is executed.
- default statement – The default statement case block will be executed when none of the above case values properly match the expression. Here default is an optional value, but, eventually it is useful to handle unexpected or undefined case statement expressions.
Example of JavaScript basic switch statement.
Let us understand a simple switch case statement program example of checking the day of a week in JavaScript program.
let day = 5; // 1: Sunday, 2: Monday, …, 7: Saturday
switch (day) {
case 1:
console.log(“Sunday”);
break;
case 2:
console.log(“Monday”);
break;
case 3:
console.log(“Tuesday”);
break;
case 4:
console.log(“Wednesday”);
break;
case 5:
console.log(“Thursday”);
break;
case 6:
console.log(“Friday”);
break;
case 7:
console.log(“Saturday”);
break;
default:
console.log(“Invalid day”);
}
Explanation of the above program.
In this program, if the day is 5, then the output will be “Thursday”.
If none of the cases in this program expression matches the value of day, then the default block code will be executed automatically.
Using switch for string matching in JavaScript program example.
In the JavaScript program, we create a switch statement to check the grade of a student, and it prints the real performance track record of the student.
let grade = “A”; // check student earn grade performance
switch (grade) {
case “A”:
console.log(“Excellent Performance”);
break;
case “B”:
console.log(“Good Performance”);
break;
case “C”:
console.log(“Average Performance”);
break;
case “D”:
console.log(“Very Bad Performance”);
break;
default:
console.log(“Unable choice”);
}
Explanation of the above program.
Here in this program the grade is “A”, so the program output will be “Excellent Performance”.
If any value other than grade “A”, “B”, “C”, or “D” is entered or checked in this program, the default case will print the message “Unable choice”.
Multiple case examples with the same code in a JavaScript program.
You can connect multiple case statements to execute the same program source code for different values in a particular JavaScript program. This condition is useful when you move multiple values towards the same result.
let status = “accepted”;
switch (status) {
case “accepted”:
console.log(“Your request is accepted.”);
break;
case “refuse”:
console.log(“Your request is refused.”);
break;
case “pending”:
case “under review”:
console.log(“Your request is processed.”);
break;
default:
console.log(“you enter invalid request”);
}
Explanation of above program.
If the condition in this program is “pending” or “under review”, then your program code will output the statement “Your request is under process”.
If the current condition is “accepted”, then it will output the message “Your request is accepted”.
If the condition here is “rejected”, then it will output “Your request is rejected”. If the condition does not match any of the switch cases, the program will execute the default block.
Example of switch with expression in JavaScript.
JavaScript programmers can use multiple expressions in a switch statement, not just a simple variable but also apply multiple loop small blocks.
let p = 9;
switch (true) {
case (p > 7):
console.log(“p is greater than 7”);
break;
case (p < 7):
console.log(“p is less than 7”);
break;
case (p === 7):
console.log(“p is equal to 7”);
break;
default:
console.log(“input value of p”);
}
Here is this program explanation.
Here in this program switch is evolving the true expression, and each switch case checks whether p satisfies any condition (p > 7, p < 7, or p === 7).
As the value of p is 9 here, so the condition p > 7 is true, and it prints the statement “p is greater than 7”.
Using switch to handle multiple options in JavaScript (Menu System) Example.
let selection = 2;
switch (selection) {
case 1:
console.log(“You chose selection 1”);
break;
case 2:
console.log(“You chose selection 2”);
break;
case 3:
console.log(“You chose selection 3”);
break;
default:
console.log(“Invalid selection”);
}
Program explanation given above.
If the current program has selection 2, the program prints the statement “You chose selection 2”.
If the selection is anything other than 1, 2, or 3, the program will output the statement “Invalid selection”.
Advantages of the switch statement in JavaScript.
Readability – Switch statements in JavaScript are often implemented in a more clear and readable format than the lengthy process of if-else statements.
Performance – In some particular conditions, a switch statement can be more efficient than a series of if-else statements. Particularly, when evaluating a single variable against many possible values.
When to use a switch – When you have a single expression or variable being compared to multiple values in a JavaScript program, a switch statement is handy to use. The switch statement is best suited for conditions or situations where you are dealing with multiple possible outputs based on the value of a program expression, such as a menu, score grade, day of the week, etc.
When not to use switch – If you are testing some particular conditions in a JavaScript program, and they are more complex, such as logical operators or multiple program variables, then usually, it is better to use the if-else statement control flow. If you need to test a series or conditions that are not directly related to a particular value, such as checking whether a value is greater than or equal to a particular value, then use the if-else control flow statement instead of the switch statement.
Conclusion of the switch case statement in JavaScript.
The switch state in a JavaScript program is a powerful tool for managing multiple possible conditions in a clear and readable order, especially when program conditions depend on a single expression or value. When you’re evaluating a variable against several different values, it can be a more efficient and organized alternative to multiple if-else statements.