In some cases where multiple nested for loops are used
Break and continue will be used to jump the loop.
Break is jumping out of the loop
Continue is to jump out of the current loop and continue the next loop
The multi-layer for loop nesting uses these two keywords to skip to the previous layer of the current for loop by default
What if we need to jump to the upper level of the previous layer, or jump to the top layer?
JavaScript provides us with a looping tab that feels a bit like a goto statement in C
1 Here :2 for(vari=0;i<5;i++){3 for(varj=0;j<5;j++){4 if(i===2&&j===2){5 BreakHere ;6 }7 Console.log (i,j);8 }9}
Here in the code above can define its own name, and it's important to see that there's a colon behind here, not a semicolon! Run to discover the output
0,00,10,20,30,41,01,11,21,31,42,02,1
You can see that when you loop to i=2,j=2, you exit the loop directly instead of just exiting the inner loop for a better contrast, and if we remove the here run behind the break here, we can see the output
0,00,10,20,30,41,01,11,21,31,42,02,13,03,1// omit 3,2~4,3
At this point, the break simply jumps out of the inner loop, and the function continues to circulate from the outer loop i=3. This output obviously has the same effect as continue here (break) The former is jumping out of the current layer loop, and the outer loop continues (continue here) The latter is to jump to the outermost loop to continue
Javascript for loop specifies anchor point jumps