JS for and other loops jump out of the multilayer loop

Source: Internet
Author: User

JS for loop jumps out of the multilayer loop

var a = [1,2,3,4,5,6,7,8]; 8 numbers
var b = [11,12,13,14,15,3,16,17]; 8 numbers

Testfor ();
Console.log (' 555 ')

function Testfor () {
for (Var k=0;k<a.length;k++) {
Console.log (' 444 ');
for (Var i=0;i<a.length;i++) {
for (Var j=0;j<b.length;j++) {
if (A[i]==b[j]) {
return false;
}
Console.log (' 111 ');
}
Console.log (' 2222 ');
}
Console.log (' 333 ');
}
}


Output:
1 plays 444
8 plays 111
1 plays 222
8 plays 111
1 plays 222
5 plays 111
1 plays 555
It is visible that return jumps directly out of the multilayer loop, returning the calling method outside
Reason: JS for is not a local scope concept, the method can be a local scope
Return will jump out of the current local function and proceed to the following method

Attention:
1. Where the For loop is executed directly under the global scope without being wrapped by a method,
Will directly lead to code written in for after will never be executed;

2. In the case of logic particularly complex multilayer loops, you will encounter some methods such as iterators,
This iterator implementation is different, and there is another case that does not jump out of any loops,
The loop still continues, except the current loop if the code is not executed once, and the next time the loop starts,
The code after if is still executed

Such as:
var cc = ' xx ';

Object.keys (o). ForEach (function (key) {
var val = O[key];
if (cc = = key) {
return false;
}
Console.log (key);
});

In addition, there are
Break
Continue
Statement
After the break statement jumps out of the loop, the code after the loop resumes (exiting the loop)
The continue continue statement interrupts the iteration in the loop, if the specified condition occurs, and then resumes the next iteration in the loop. (Skip the current iteration and go to the next iteration)
These two statements can specify a label so that you can exit a specific loop
Such as
Bbq
for (Var j=0;j<a.length;j++) {
Ccc:
for (var i =0;i<a.length;i++) {
if (i==5) {
Break BBQ; Just jump out of the BBQ outer loop.
}
}
}


Or:
function Testfor () {
Bbq
for (Var k=0;k<a.length;k++) {
Console.log (' 444 ');
Ccc:
for (Var i=0;i<a.length;i++) {
Ddd:
for (Var j=0;j<b.length;j++) {
if (j = = 2) {
Break
}
Console.log (' J ' +j);
}
Console.log (' I ' +i);
}
Console.log (' K ' +k);
}
}

The DDD loop is only exited each time the loop is j==2, and the outer loop will continue to loop.

JS for and other loops jump out of the multilayer loop

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.