JavaScript continue vs. break

October 9, 2015

Two very similar statements in JavaScript that should not be confused are _continue__ _and break. First let’s talk about their similarities.

Similarities

  • Both continue and break only work inside of loop, switch, or label statements.
  • They are both statements so they are called without parenthesis as in “continue;” or “break;”
  • They both terminate the current execution.

Differences

Continue only terminates the current iteration whereas break terminates the execution of the entire statement. The difference is easier to see with code examples!

Continue just skips 2 and continues going through the for loop.

for (var i = 0; i < 10; i++) {
  if (i === 2) {
    continue;
  }
  console.log(i);
}

// This returns
// 0
// 1
// 3
// 4
// 5
// 6
// 7
// 8
// 9

Break stops the entire loop and returns execution control to the program.

for (var i = 0; i < 10; i++) {
  if (i === 2) {
    break;
  }
  console.log(i);
}

// This returns
// 0
// 1