Usage examples for JavaScript switch cases [scope]_javascript tips

Source: Internet
Author: User
Tags case statement switch case
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]
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.