JS in the use of break continue and return?

Source: Internet
Author: User

In Break,continue and return three keywords, break,continue is together, return is a function return statement, but also return the function to stop

Break and Continue:

Exiting a loop or switch statement that is used elsewhere can cause errors

Break

Example:

For (var i=1;i<=10;i++)

{

if (i==6)

{

Break

}

document.write (i);

}//Output result: 12345

When I=6, exit for this loop directly. This loop will no longer be executed!

Continue

is similar to the break statement. What's different is that it's not quitting a loop, but starting a new iteration of the loop. The continue statement can only be used in the loop body of a while statement, Do/while statement, for statement, or For/in statement, and will cause an error in other places!

Example:

For (var i=1;i<=10;i++)

{

if (i==6) continue;

document.write (i);

}//Output result: 1234578910

When I=6, just jump out of this for loop. Continue the execution next time.

Return statement:

The return statement is used to specify the value returned by the function.

The return statement can only be applied to the body of the function, and anywhere else in the code can cause a syntax error!

First in JS, we often use return false to prevent the submission of the form or continue to execute the following code, in general, to prevent the execution of the default behavior

In summary: Return false is valid only in the current function and does not affect the execution of other external functions.

First, return control and function results,

The syntax is: return expression; Statement End Function executes, returns the calling function, and takes the value of the expression as the result of the function

Second, return control,

No function result, the syntax is: return;

In most cases, false is returned for the event handler to prevent the default event behavior.

For example, by clicking on a <a> element by default, the page jumps to the page specified by the href attribute of the element. Return False equals the Terminator, and return True is the equivalent of the executor.

The function of return false in JS is generally used to cancel the default action. For example, if you click a link to trigger a default event other than triggering your onclick time (if you specify), the jump to the page is performed. So if you want to cancel the object's default action, you can return false.

JS in the use of break continue and return?

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.