JS Switch

The switch statement is used to execute one block of code among many options. It evaluates an expression, matches the result to a case, and executes the associated block.


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 no 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");
}

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.