Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The continue statement is a control flow mechanism used within loop structures to prematurely terminate the execution of the current iteration. When the JavaScript engine encounters a continue statement, it bypasses all subsequent statements remaining in the loop’s body for that specific iteration and immediately transfers control to the loop’s next iteration cycle.

Syntax

continue;
continue labelIdentifier;

Execution Mechanics

The exact behavior of the control flow transfer depends on the type of loop enclosing the continue statement:
  • for loop: Control jumps directly to the loop’s update expression (e.g., i++). After the update expression executes, the loop’s condition is evaluated to determine if the next iteration should proceed.
  • while and do...while loops: Control jumps directly to the loop’s condition evaluation.
  • for...in and for...of loops: Control advances to the next enumerable property or iterable value in the sequence.

Unlabeled continue

When used without a label, continue applies strictly to the innermost enclosing loop.
for (let i = 0; i < 4; i++) {
  if (i === 2) {
    continue; // Control jumps to i++
  }
  console.log(i);
}
// Output: 0, 1, 3

Labeled continue

JavaScript allows statements to be prefixed with a label identifier. When continue is paired with a label, it bypasses the current iteration of the specific loop associated with that label, rather than defaulting to the innermost loop. This is primarily utilized in nested loop architectures.
outerLoop: for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === 1 && j === 1) {
      continue outerLoop; // Control jumps to i++ of the outer loop
    }
    console.log(`i=${i}, j=${j}`);
  }
}
// Output:
// i=0, j=0
// i=0, j=1
// i=0, j=2
// i=1, j=0
// i=2, j=0
// i=2, j=1
// i=2, j=2

Technical Constraints

  1. Scope Restriction and Function Boundaries: A continue statement must be nested within an iterative statement (while, do...while, for, for...in, or for...of). Crucially, continue cannot cross function boundaries. Using continue inside a nested function—such as a callback passed to .forEach(), .map(), or setTimeout()—will throw a SyntaxError, even if that function is physically defined or executed inside a loop.
for (let i = 0; i < 3; i++) {
  [1, 2].forEach(num => {
    if (num === 1) {
      continue; // Throws SyntaxError: Illegal continue statement
    }
  });
}
  1. Automatic Semicolon Insertion (ASI): The continue statement is subject to JavaScript’s ASI rules. No line terminator (line break) is permitted between the continue keyword and its target labelIdentifier. If a line break exists, the engine will automatically insert a semicolon after continue. This does not throw a SyntaxError, but it creates a logical error by executing an unlabeled continue and rendering the subsequent label an unreachable expression statement.
// Logical error due to ASI
continue 
  targetLabel; 

// The engine parses the above as syntactically valid but logically flawed:
// continue;     <- Unlabeled continue executes here
// targetLabel;  <- Unreachable expression statement
Master JavaScript with Deep Grasping Methodology!Learn More