JS jumps out of the Loop method difference (break, continue, return) and $.each (return True and return false)

Source: Internet
Author: User
Tags case statement

The break statement for JS programming syntax:

The break statement causes the running program to immediately exit the loop that is contained in the inner layer or exit a switch statement.

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

If the termination condition of a loop is very complex, it is much easier to use the break statement to implement certain conditions than to express all the conditions with a loop expression.

for (Var i=1;i<=10;i++) {     if (i==8) {break         ;     }     document.write (i); }

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

output Result: 1234567

JS programming syntax of the CONTINUE statement:

The continue statement 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!

for (Var i=1;i<=10;i++) {     if (i==8) {         continue;     }     document.write (i); }

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

Output results: 1234567910

The return statement of the JS programming syntax:

The return statement is used to specify the value returned by the function. The return statement can only appear in the function body, and anywhere else in the code will cause a syntax error!

for (Var i=1;i<=10;i++) {     if (i==8) {         return;     }     document.write (i); }

Execution result uncaught syntaxerror:illegal return statement (...)
This means that an illegally captured query returns a statement.

When you execute a return statement, the function execution stops even if there are other statements in the body of the function !

if (username== ") {   alert (" Please enter user name ");   return false;} if (qq== "") {   alert ("Please enter QQ");   return false;}

In the above example, when the username is empty, it will not be executed down again, in some form submissions, you can also stop the default submission method by return false, instead of the Ajax submission method, for example:

<form id= "form" onsubmit= "return false" >...</form>

return false Jump out of the loop and return true  continue the loop

Summarize:

1, for loop we use continue, terminate this cycle count into the next cycle. Use break to terminate the entire loop.
2. In jquery, $.each uses return true and return False.

Break can jump out of the Switch...case statement and proceed to the contents after the switch statement. The break statement can also jump out of the loop, that is, to end the execution of the loop statement.

The function of the continue statement is to end the loop, and then the next time the loop is executed.

The difference between continue and break is that break is the complete end of the loop, and continue is the end of the cycle

.

JS jumps out of the loop by the method difference (break, continue, return) and $.each (return True and return false)

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.