JavaScript Reference ยท Statements and Declarations
The continue statement
Reference for the JavaScript continue statement: skipping the rest of a loop iteration, labeled continue for nested loops, and how it differs in each loop type.
The continue statement ends the current iteration of a loop and jumps to the next one. In a for loop it goes to the update expression and then the condition; in while and do...while it goes to the condition; in for...of and for...in it takes the next item. With a label it continues an outer loop.
It is useful for "guard clauses" in loops: skip uninteresting items at the top so the main work is not nested in an if.
for (const line of lines) { if (line.trim() === "") { continue; } process(line); }
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
continue; continue label;
| Part | Description |
|---|---|
label |
Optional. The label of an enclosing loop to continue. Must be on the same line as continue. |
Exceptions: a SyntaxError if continue is not inside a loop, or if the label does not name an enclosing loop.
Usage notes
- Loops only. Unlike
break,continuecannot be used in aswitchalone or a plain block; it must be inside a loop (aswitchinside a loop is fine and continues the loop). - What happens next depends on the loop:
forruns the update expression first;whileanddo...whilego straight to the condition;for...ofandfor...infetch the next item. whiletrap. If the variable that the condition checks is changed after thecontinueline, skipping it can create an infinite loop. Update first or usefor.- Labeled continue resumes the labeled outer loop, running its next iteration, which replaces flag variables in nested loops.
- Callbacks.
continuedoes not work insideforEachcallbacks; usereturnthere to skip the current item. - Finally blocks run before control leaves a
tryviacontinue. - Style.
continueat the top of a loop body flattens nesting. Too manycontinue/breakstatements in one loop can make flow hard to follow; considerfilter().
Browser support
Supported in every JavaScript engine.
Related
break,label,for,while- Lessons: break and continue, while loops