1switch(expression) {
2 case x:
3 // code block
4 break;
5 case y:
6 // code block
7 break;
8 default:
9 // code block
10}
1switch (a) {
2 case 1:
3 alert('case 1 executed');
4 break;
5 case 2:
6 alert("case 2 executed");
7 break;
8 case 3:
9 alert("case 3 executed");
10 break;
11 case 4:
12 alert("case 4 executed");
13 break;
14 default:
15 alert("default case executed");
16}
1//javascript multiple case switch statement
2var color = "yellow";
3var darkOrLight="";
4switch(color) {
5 case "yellow":case "pink":case "orange":
6 darkOrLight = "Light";
7 break;
8 case "blue":case "purple":case "brown":
9 darkOrLight = "Dark";
10 break;
11 default:
12 darkOrLight = "Unknown";
13}
14
15//darkOrLight="Light"
1switch(type) {
2 case "SomeString":
3 // Functionality
4 break;
5 case "OtherString":
6 // Functionality
7 break;
8 default:
9 // Functionality
10 break;
11}
1function whatToDrink(time){
2 var drink ;
3 switch (time) {
4 case "morning":
5 drink = "Tea";
6 break;
7 case "evening":
8 drink = "Shake";
9 break;
10 default:
11 drink="Water";
12 }
13 return drink;
14}
15console.log(whatToDrink("morning")) //Tea
16console.log(whatToDrink("evening")) //Shake
17console.log(whatToDrink("night")) //Water
18console.log(whatToDrink("daytime")) //Water
1switch (expression) {
2 case value1:
3 //Statements executed when the
4 //result of expression matches value1
5 [break;]
6 case value2:
7 //Statements executed when the
8 //result of expression matches value2
9 [break;]
10 ...
11 case valueN:
12 //Statements executed when the
13 //result of expression matches valueN
14 [break;]
15 [default:
16 //Statements executed when none of
17 //the values match the value of the expression
18 [break;]]
19}