Break,continue and Return Statement usage Summary _javascript tips in JavaScript

Source: Internet
Author: User
Since you've been looking at JavaScript lately, here's a quick look at some of the three uses of JavaScript

Break statement:
The break statement causes the running program to immediately exit the loop that contains the inner layer or exit a switch statement. Because it is used to exit loops or switch statements, this form of break statement is legitimate only if it appears in these statements.

If the termination condition of a loop is very complex, it is much easier to use a break statement to implement certain conditions than to express all the conditions with a circular expression.
<script type= "Text/javascript" > for (var i=1;i<=10;i++) {if (i==6) break; document.write (i); ///Output result: 12345 </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Continue statement:
The continue statement is similar to the break statement. The difference is that instead of exiting a loop, it is a new iteration that begins the loop.

Continue statements can only be used in a while statement, a do/while statement, a for statement, or a loop body of a for/in statement, which can cause an error when used elsewhere!
<script type= "Text/javascript" > for (var i=1;i<=10;i++) {if (i==6) continue; document.write (i); ///Output result: 1234578910 </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


Return statement:
The return statement is used to specify the value returned by the function. The return statement can only appear in the body of the function, and any other place in the code can cause a syntax error!
When the return statement is executed, function execution stops even if there are other statements in the function body!
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.