JS in the Exit statement break,continue and return comparison (reprint)

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

First of all: break and continue two discussion together

The scope of these two applications is to exit the loop or switch statement, which can cause errors in other places;

Break: Because it is used to exit a loop or switch statement, this form of break statement is valid only if it appears in these statements.

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: 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.

A third 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, 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, you click on a link to trigger your

The onclick time (if you specify it) to trigger a default event is to perform a jump on the page. So if

You can return FALSE if you want to cancel the object's default action.

First in JS, we often 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.

function A () {

if (True)

return false;

}, there is no problem with this.

If I change into this

function Test () {

A ();

b ();

C ();

}

Even if the a function returns false to prevent the commit, it does not affect the execution of the B () and C () functions. Call the A () function in the test () function.

Return false is equivalent to the return value for the test () function. The test () function cannot be prevented from executing.

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

Summarize

Retrun true; Returns the correct processing result.

return false; Branch error processing result, terminate processing.

return; Returns control to the page.

JS in the Exit statement break,continue and return comparison (reprint)

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.