xxxxxxxxxx
switch(expression) {
case x:
// code of block to be executed when this case matches
break; // if you decide to run a return statement in a specific case, then don't write break
case y:
// code of block to be executed when this case matches
return "xyz"
// In this case no "break" will be written as we have written a return statement
default: "xyz" // If no case matches then the code written in the block of "default" will be executed
}
// For example
let x = 5;
switch(x) {
case 1:
console.log(`x value is ${x}`)
case 2:
console.log(`x value is ${x}`)
case 3:
console.log(`x value is ${x}`)
case 4:
console.log(`x value is ${x}`)
case 5:
console.log(`x value is ${x}`)
}
// Output "x value is 5"
xxxxxxxxxx
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
xxxxxxxxxx
//javascript multiple case switch statement
var color = "yellow";
var darkOrLight="";
switch(color) {
case "yellow":case "pink":case "orange":
darkOrLight = "Light";
break;
case "blue":case "purple":case "brown":
darkOrLight = "Dark";
break;
default:
darkOrLight = "Unknown";
}
//darkOrLight="Light"
xxxxxxxxxx
var color = "red";
switch (color) {
case "blue":
console.log("color is blue");
break;
case "white":
console.log("color is white");
break;
case "black":
console.log("color is black");
break;
case "red":
console.log("color is red");
break;
default:
console.log("color doesn't match ")
}
//Output: color is red;
xxxxxxxxxx
function whatToDrink(time){
var drink ;
switch (time) {
case "morning":
drink = "Tea";
break;
case "evening":
drink = "Shake";
break;
default:
drink="Water";
}
return drink;
}
console.log(whatToDrink("morning")) //Tea
console.log(whatToDrink("evening")) //Shake
console.log(whatToDrink("night")) //Water
console.log(whatToDrink("daytime")) //Water
xxxxxxxxxx
var color = prompt("enter color");
switch(color) {
case "red":
console.log("stop")
break;
case "green":
console.log("go head")
break;
case "yellow":
console.log("get ready")
break;
default:
console.log("enter valid color")
}
xxxxxxxxxx
let x = 3;
switch(x) {
case 1:
console.log("You entered 1");
break;
case 2:
console.log("You entered 2");
break;
default:
console.log("You entered: "+ x);
}
xxxxxxxxxx
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
}
console.log(day);
xxxxxxxxxx
switch(x){
case value1: // if x === value1
break;
case value2: // if x === value2
break;
default: // if x not match
}
xxxxxxxxxx
var foo = 0;
switch (foo) {
case -1:
console.log('1 negativo');
break;
case 0: // foo es 0, por lo tanto se cumple la condición y se ejecutara el siguiente bloque
console.log(0)
// NOTA: el "break" olvidado debería estar aquí
case 1: // No hay sentencia "break" en el 'case 0:', por lo tanto este caso también será ejecutado
console.log(1);
break; // Al encontrar un "break", no será ejecutado el 'case 2:'
case 2:
console.log(2);
break;
default:
console.log('default');
}