JavaScript Language 12 and javascript Language 12

Source: Internet
Author: User

JavaScript Language 12 and javascript Language 12

When using the if statement, if you encounter many conditions, you should not continue using the if statement. JavaScript provides a more efficient alternative, that is, the switch statement, let's first look at the switch statement template:

<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=1;switch(value1){case 0:document.write("value1=0");break;case 1:document.write("value1=1");break;case 2:document.write("value1=2");break;default:document.write("value1="+value1);break;}</SCRIPT></BODY></HTML>


The switch statement consists of the following parts:

Conditional expressions, case statements, break statements, and default statements.

1) The switch statement starts with the switch keyword, placing the condition expression in parentheses after the switch keyword.

2) The role of the case statement is to check whether the condition matches (the switch statement can contain N multiple case statements ).

3) The break statement is used to tell JavaScript to stop execution and switch the statement. Of course, you can also omit the break statement. If it is omitted, the statement continues until the break statement or switch statement is executed.

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


Code after the break statement is omitted:

<HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=1;switch(value1){case 0:document.write("value1=0"+"<br>");case 1:document.write("value1=1"+"<br>");case 2:document.write("value1=2"+"<br>");default:document.write("value1="+value1);break;}</SCRIPT></BODY></HTML>



After the break statement is omitted, we can see that the above is executed downward from case 1 until the break statement at the default statement is stopped (of course, if the break statement from the default statement is also omitted, results are the same as above ).

If the case statement and break statement are reasonably matched, some better logic code can be written.




Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41318865 sentiment control _


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.