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:
-
Define a
wordvariable and initialize it with the string'javascript'. -
Define an
indexvariable and initialize it with the integer0. -
Define a
whileloop that will run for as long asindexpoints to a valid character inword.- Stop the loop's execution if the character at
indexis an's'. - Otherwise, output the current character and increment
indexby1to move to the next character in the string.
- Stop the loop's execution if the character at
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:
-
Define a
forloop.- Define a
numbervariable and initialize it with0. - Check if
numberis less than or equal to10before executing the loop. - Skip the current iteration if
numberis divisible by2. - Output the value of
number. - Increment the value of
numberby1.
- Define a
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