JavaScript Language Basics 12

Source: Internet
Author: User
Tags case statement

When using the IF statement, if you encounter many conditions, you should not continue to use the IF statement, JavaScript provides a more efficient alternative, that is the switch statement, we first look at the switch statement template:


In the above we can see that the switch statement consists of the following parts:

Conditional expression, Case statement, break statement, default statement.

1) The switch statement starts with the keyword switch and places the conditional expression in parentheses after the Switch keyword.

2) The purpose of the case statement is to check whether the condition matches (the switch statement can contain more than n case statements).

3) The action of the break statement is to tell JavaScript to stop execution here and jump off the switch statement. Of course, you can omit the break statement, after omitting, the statement will continue to execute until the break statement or switch statement execution is complete.

4) The default statement indicates that all case statements do not match when they are executed, and of course the default statement can be omitted.


The code after the break statement is omitted:

Omitting the break statement shows that the above is done from Case 1 down until the break statement at the default statement is stopped (and the break statement of the default statement is omitted, the result is the same as above).

If a reasonable collocation of the case statement and the location of the break statement can write some better logic code.




Reprint Please specify the Source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41318865 Emotional Control _


JavaScript Language Basics 12

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.