1switch(expression) {
2 case x:
3 // code block
4 break;
5 case y:
6 // code block
7 break;
8 default:
9 // code block
10}
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"
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 (expr) {
2 case 'Oranges':
3 console.log('Oranges are $0.59 a pound.');
4 break;
5 case 'Apples':
6 console.log('Apples are $0.32 a pound.');
7 break;
8 case 'Bananas':
9 console.log('Bananas are $0.48 a pound.');
10 break;
11 case 'Cherries':
12 console.log('Cherries are $3.00 a pound.');
13 break;
14 case 'Mangoes':
15 case 'Papayas':
16 console.log('Mangoes and papayas are $2.79 a pound.');
17 break;
18 default:
19 console.log('Sorry, we are out of ' + expr + '.');
20}
21
22console.log("Is there anything else you'd like?");
23
1function caseInSwitch(val) {
2 var answer = "";
3 // Only change code below this line
4switch(val) {
5 case 1:
6 answer = "alpha";
7 console.log(answer);
8 break;
9 case 2:
10 answer = "beta";
11 break;
12 case 3:
13 answer = "gamma";
14 break;
15 case 4:
16 answer = "delta";
17 break;
18}
19
20
21 // Only change code above this line
22 return answer;
23}
24
25caseInSwitch(1);
26
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}