JS switch
switch statement selects the matched case out of multiple conditions to execute the specific codes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
x=2;
switch (x)
{
case 1:
x += 5;
alert(x);
break;
case 2:
x += 10;
alert(x);
break;
case 3:
x += 15;
alert(x);
break;
default:
x += 20;
alert(x);
break;
}
The break statement stops all statements after the matched case. If
no break statement, all scripts after the matched case will be executed.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
x=2;
switch (x)
{
case 1:
x += 5;
alert(x);
case 2:
x += 10;
alert(x);
case 3:
x += 15;
alert(x);
default:
x += 20;
alert(x);
}