JavaScript Switch
Last Updated on May 06, 2022JavaScript switch statement.
The JavaScript switch statement is used to check and perform different actions based on the different conditions. It is much like an if-else statement, but it can be used to execute one code from multiple expressions.
The JavaScript switch statement examples are given below.
switch(expression) {
case case1:
// execute code block
break;
case case2:
// execute code block
break;
default:
// execute code block if nothing matched
}
let bestProgrammingLang = "JavaScript";
switch(bestProgrammingLang) {
case "JavaScript":
console.log("JavaScript Lover!");
break;
case "CSharp":
console.log("CSharp Lover!");
break;
default:
console.log("Not sure yet!");
}
//Output: "JavaScript Lover!"
let bestProgrammingLang = null;
switch(bestProgrammingLang) {
case "JavaScript":
console.log("JavaScript Lover!");
break;
case "CSharp":
console.log("CSharp Lover!");
break;
default:
console.log("Not sure yet!");
}
//Output: "Not sure yet!"