See the Pen thecodelog.com - JS Loop Statements 1 by Deano (@deangilewicz) on CodePen.
The value of label may be any JavaScript identifier that is not a reserved word. The statement that you identify with a label may be any statement.
See the Pen thecodelog.com - JS Loop Statements 2 by Deano (@deangilewicz) on CodePen.
A break statement terminates a loop and switch, and can also be used with a label.
See the Pen thecodelog.com - JS Loop Statements 3 by Deano (@deangilewicz) on CodePen.
When break is used without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following statement.
See the Pen thecodelog.com - JS Loop Statements 4 by Deano (@deangilewicz) on CodePen.
When you use break with a label, it terminates the specified labeled statement. Below, the ‘outer loop’ will log 0 then the ‘inner loop’ will log 1 to 9 then break when z = 10 back to the ‘labelCancelLoops’ loop
See the Pen thecodelog.com - JS Loop Statements 5 by Deano (@deangilewicz) on CodePen.
The continue statement can be used to restart a while, do-while, and for loop, or a label statement.
See the Pen thecodelog.com - JS Loop Statements 6 by Deano (@deangilewicz) on CodePen.
When continue is used without a label, it terminates the current iteration of the innermost enclosing while, do-while, or for loop and continues execution of the loop with the next iteration. Unlike the break statement, continue does not terminate the execution of the loop entirely.
See the Pen thecodelog.com - JS Loop Statements 7 by Deano (@deangilewicz) on CodePen.
When continue is used with a label, it applies to the looping statement identified with that label.
See the Pen thecodelog.com - JS Loop Statements 8 by Deano (@deangilewicz) on CodePen.
A statement labeled ‘outer’ contains a statement labeled ‘inner’. When continue is encountered, the program terminates the current iteration of ‘inner’ and begins the next iteration of ‘inner’. Each time continue is encountered, ‘inner’ reiterates until its condition returns false. When false is returned, the remainder of the ‘outer’ statement is completed, and ‘outer’ reiterates until its condition returns false.
Here the ‘inner’ label contains a continue statement that has a label of ‘outer’, which causes the program to continue at the top of the ‘outer’ statement.
See the Pen thecodelog.com - JS Loop Statements 9 by Deano (@deangilewicz) on CodePen.