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"
1let color = "black";
2
3switch(color){
4 case "red":
5 console.log("color is red");
6 break;
7 case "white":
8 console.log("color is white");
9 break;
10 case "black":
11 console.log("color is black");
12 break;
13 default:
14 console.log("unknow color");
15}
1var myvalue='val1';//or other values; val2 val3 valx
2switch(myvalue) {
3 case 'val1':
4 console.log('var myvalue is '+ myvalue);
5 //other code ...
6 break;
7 case 'val2':
8 console.log('var myvalue is '+ myvalue);
9 //other code ...
10 break;
11 default:
12 // when all the other values not covered with cases above
13 // other code ...
14}
1switch(expression) {
2 case a:
3 // case logic
4 break;
5 case b:
6 // case logic
7 break;
8 default:
9 // default logic
10}