several examples:
Copy Code code as follows:
function Case1 (num) {
Switch (num) {
Case 1:
Document.writeln ("show 1!!");
Break
Case 2:
Document.writeln ("show 2!!");
Break
Case 3:
Document.writeln ("show 3!!");
Break
Default
Document.writeln ("show others!!");
Break
}
}
function Case2 (num) {
Switch (num) {
Case 1:
Document.writeln ("show 1!!");
No break, so we'll continue with Case 2.
Case 2:
Document.writeln ("show 2!!");
Break
Case 3:
Document.writeln ("show 3!!");
No break, so we'll continue with case 4.
Case 4:
Document.writeln ("show 4!!");
Break
Default
Document.writeln ("show others!!");
Break
}
}
function Case3 (num) {
Switch (num) {
Case 1:
Case 2:
Relative to if (Num==1 | | num==2)
Document.writeln ("Show 1 or 2!!");
Break
Case 3:
Case 4:
Relative to if (num==3 | | num==4)
Document.writeln ("Show 3 or 4!!");
Break
Default
As if Else
Document.writeln ("show others!!");
Break
}
}
function Case4 (num) {
Switch (f (num)) {
Case 1:
Case 2:
Relative to if (Num==1 | | num==2)
Document.writeln ("Show 1 or 2!!");
Break
Case 3:
Case 4:
Relative to if (num==3 | | num==4)
Document.writeln ("Show 3 or 4!!");
Break
Default
As if Else
Document.writeln ("show others!!");
Break
}
}
function f (num) {
return num;
}
function case5 (num) {
Switch (num<=2) {
Case true:
Document.writeln ("num <= 2");
Break
Case false:
Document.writeln ("num > 2");
Break
}
}
JavaScript Switch Case statement set scope
<script> var x=10 switch (true) {case X>0&&x<10:alert (1); Break Case X>=10&&x<20:alert (2); Break } </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]