Use switch in JS

Source: Internet
Author: User
Grammar
Switch (n) {Case 1:  Execute code block 1  break;case 2:  Execute code block 2  break;default:  N code that executes when different from 1 and 2

How it works: first set an expression n (usually a variable). The value of the subsequent expression is compared to the value of each case in the structure. If there is a match, the code block associated with the case is executed. Use break to prevent your code from automatically running down a case.

Instance

Displays the week name for today. Please note that sunday=0, Monday=1, tuesday=2, etc.:

var day=new Date (). GetDay (); switch (day) {case 0:  x= "Today it ' s Sunday";  Break;case 1:  x= "Today it ' s Monday";  Break;case 2:  x= "Today it ' s Tuesday";  Break;case 3:  x= "Today it ' s Wednesday";  Break;case 4:  x= "Today it ' s Thursday";  Break;case 5:  x= "Today it ' s Friday";  Break;case 6:  x= "Today it ' s Saturday";  Break

}

Use switch in JS

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.