The switch statement in JavaScript
Switch Statement
A switch statement is similar to the if, if-else, if else-if else statements. The difference is that in that scenario, we have to add multiple else if statements while in this case, we have to add a new case. The switch expression is evaluated once. The value of the expression is compared with the values of each case, and if there’s a match, that block of code is executed.
The break Keyword
When the right cases match with the value then that particular case will be executed. However, all remaining cases which are not tested will be tested. To prevent this, we use the break keyword. The break keyword essentially switches the switch statement off.
The default Keyword
When no cases match the value then the default case will be executed. This case is present at the end of every switch statement. We use the default keyword, which specifies the code to run if there’s no case match.
Syntax:-
switch(case){
case "condition_1":
// code here will be executed
break;
case "condition_2":
// code here will be executed
break;
case "condition_3":
// code here will be executed
break;
default:
// default code will be executed
}