Control Loop Iterations in JavaScript

4 min read·Jan 1, 2026

In JavaScript, the break and continue statements allow you to control the execution flow of loops by either terminating them prematurely or skipping their iterations.

The break keyword

The break keyword is used to immediately terminate a running loop, regardless of whether the loop's condition is met or not.

loop {
  statements
  break;
}

Example

Let's consider this script, that outputs the individual characters of a string until it encounters the 's' character:

const word = 'javascript';
let index = 0;

while (word[index] !== undefined) {
  if (word[index] === 's') {
    break;
  }
  console.log(word[index]);
  index++;
}

When executed, it will:

  1. Define a word variable and initialize it with the string 'javascript'.

  2. Define an index variable and initialize it with the integer 0.

  3. Define a while loop that will run for as long as index points to a valid character in word.

    1. Stop the loop's execution if the character at index is an 's'.
    2. Otherwise, output the current character and increment index by 1 to move to the next character in the string.

Which will produce this output:

j
a
v
a

The continue keyword

The continue keyword is used to skip the current iteration of a loop and directly proceed to the next iteration, without executing the remaining statements.

loop {
  if (condition) {
    continue;
  }
  statements
}

Example

Let's consider this script, that outputs all the odd numbers from 0 to 10:

for (let number = 0; number <= 10; number++) {
  if (number % 2 === 0) {
    continue;
  }
  console.log(number);
}

When executed, it will:

  1. Define a for loop.

    1. Define a number variable and initialize it with 0.
    2. Check if number is less than or equal to 10 before executing the loop.
    3. Skip the current iteration if number is divisible by 2.
    4. Output the value of number.
    5. Increment the value of number by 1.

Which will produce this output:

1
3
5
7
9

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Control Loop Iterations in JavaScript | Backend Brewery