How can I stop a loop early or skip one iteration?
Quick answer
The question, the details, the next step.
A practical checklist
Decide whether the loop should end or only skip its current pass.
Check that the condition matches the value or situation you intend.
Confirm which statements are skipped when control reaches
breakorcontinue.For a
forloop usingcontinue, account for the update before the next condition check.Trace a few iterations and compare them with the expected output.
A few more useful answers
Does `continue` stop the loop?
No. It skips the rest of the current iteration and proceeds toward the next one; it does not terminate the loop. [S2]
Does `break` exit every loop when loops are nested?
An unlabeled break exits the innermost loop it is in, not all enclosing loops. [S1]
Sources
References used for this guide.
- [S1] break — JavaScript | MDN developer.mozilla.org · Checked
- [S2] continue — JavaScript | MDN developer.mozilla.org · Checked
