Examples:
Copy codeThe Code is 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 !! ");
// If there is no break, the response is rejected. case 2
Case 2:
Document. writeln ("show 2 !! ");
Break;
Case 3:
Document. writeln ("show 3 !! ");
// There is no break, so it will parse the rows case 4
Case 4:
Document. writeln ("show 4 !! ");
Break;
Default:
Document. writeln ("show others !! ");
Break;
}
}
Function case3 (num ){
Switch (num ){
Case 1:
Case 2:
// Corresponds to if (num = 1 | num = 2)
Document. writeln ("show 1 or 2 !! ");
Break;
Case 3:
Case 4:
// Corresponds to if (num = 3 | num = 4)
Document. writeln ("show 3 or 4 !! ");
Break;
Default:
// Corresponds to else
Document. writeln ("show others !! ");
Break;
}
}
Function case4 (num ){
Switch (f (num )){
Case 1:
Case 2:
// Corresponds to if (num = 1 | num = 2)
Document. writeln ("show 1 or 2 !! ");
Break;
Case 3:
Case 4:
// Corresponds to if (num = 3 | num = 4)
Document. writeln ("show 3 or 4 !! ");
Break;
Default:
// Corresponds to 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 setting range
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]