js switch else code example
Example 1: js switch case
switch(expression) {
case x:
break;
case y:
break;
default:
}
Example 2: Javascript switch case code format
switch(type) {
case "SomeString":
break;
case "OtherString":
break;
default:
break;
}
Example 3: javascript switch
let color = "black";
switch(color){
case "red":
console.log("color is red");
break;
case "white":
console.log("color is white");
break;
case "black":
console.log("color is black");
break;
default:
console.log("unknow color");
}
Example 4: switch case in javascript
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"))
console.log(whatToDrink("evening"))
console.log(whatToDrink("night"))
console.log(whatToDrink("daytime"))