JS switch case condition code example
Example 1: js switch case
switch(expression) {
case x:
break;
case y:
break;
default:
}
Example 2: 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 3: js convert if/else statement to switch case
var gradeOut = 70;
var totalGrade = 80;
if (gradeOut >= 90)
gradeOut+= "My letter grade is: A";
else if (totalGrade < 90 && totalGrade >= 80)
gradeOut+= "My letter grade is: B";
else if (totalGrade < 80 && totalGrade >= 70)
gradeOut+= "My letter grade is: C";
else if (totalGrade < 70 && totalGrade >= 60)
gradeOut+= "My letter grade is: D";
else if (totalGrade < 60)
gradeOut+= "My letter grade is: F";
switch(true) {
case gradeOut<60:
gradeOut= "My letter grade is: F";
break;
case gradeOut<70:
gradeOut= "My letter grade is: D";
break;
case gradeOut<80:
gradeOut="My letter grade is: C";
break;
}
Example 4: switch case in js
var coll1="kjc;
switch(coll1)
{
case "kjc":
alert("youre special");
break;
case "horizon":
alert("worst behaviour");
break;
case "cmr" :
alert("worst character");
break;
default:
alert("kjc is best");
break;
}