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}
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
1switch (expression) {
2 case valeur1:
3 // Instructions à exécuter lorsque le résultat
4 // de l'expression correspond à valeur1
5 instructions1;
6 [break;]
7 case valeur2:
8 // Instructions à exécuter lorsque le résultat
9 // de l'expression correspond à valeur2
10 instructions 2;
11 [break;]
12 ...
13 case valeurN:
14 // Instructions à exécuter lorsque le résultat
15 // de l'expression à valeurN
16 instructionsN;
17 [break;]
18 [default:
19 // Instructions à exécuter lorsqu'aucune des valeurs
20 // ne correspond
21 instructions_def;
22 [break;]]
23}