Example of JavaScript switch case usage [range]

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.