Wednesday 8 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...