Wednesday, May 8, 2019

How to break nested loops in JavaScript?





I tried this:



for(i = 0; i < 5; i++){
for(j = i + 1; j < 5; j++){
break(2);
}

alert(1);
}


only to get:




SyntaxError: missing ; before statement





So, how would I break a nested loop in JavaScript?


Answer



You should be able to break to a label, like so:



function foo ()
{
dance:
for(var k = 0; k < 4; k++){
for(var m = 0; m < 4; m++){
if(m == 2){

break dance;
}
}
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...