continue in for loop JavaScript
continue in for loop JavaScript is the continue statement inside a for loop. It skips the rest of the current iteration. The loop then runs its update expression and tests the condition again. The loop does not end.
const out = [];
for (let i = 0; i < 10; i++) {
if (i === 5) {
continue;
}
out.push(i);
}
The loop sets i to 0. The condition i < 10 is true, i === 5 is false, and out.push(i) stores 0. The update i++ runs, so i is 1. The same path stores 1, 2, 3, and 4. When i is 5, continue skips out.push(i). The update still runs, so i becomes 6, and the condition is tested again. The loop stores 6, 7, 8, and 9. When i is 10 the condition is false and the loop stops. out is [0, 1, 2, 3, 4, 6, 7, 8, 9].
A second condition skips more than one value. This loop stores only the odd numbers:
const odds = [];
for (let i = 0; i < 8; i++) {
if (i % 2 === 0) {
continue;
}
odds.push(i);
}
i runs from 0 through 7. On each even value, i % 2 === 0 is true and continue skips odds.push(i). The update i++ still runs. odds is [1, 3, 5, 7].
The same shape skips empty entries in an array:
const values = ["ada", "", null, "grace"];
const names = [];
for (let i = 0; i < values.length; i++) {
const value = values[i];
if (value === null || value === undefined || value === "") {
continue;
}
names.push(value);
}
Index 0 is "ada", so that string is pushed. Index 1 is "", and index 2 is null, so both are skipped. Index 3 is "grace", which is pushed. names is ["ada", "grace"]. values is unchanged.
continue in a while loop
In a while loop, continue jumps to the condition. There is no update expression. If the only line that moves the counter sits below continue, the condition never changes and the loop never ends.
let i = 0;
while (i < 5) {
if (i === 3) {
continue;
}
i++;
}
i becomes 1, then 2, then 3. The next time the condition i < 5 passes, i === 3 is true and continue goes back to that condition. i++ does not run. i stays 3, so this loop does not finish.
Move the increment above continue:
let i = 0;
const kept = [];
while (i < 5) {
i++;
if (i === 3) {
continue;
}
kept.push(i);
}
i becomes 1 and is stored, then 2 is stored. i becomes 3, continue skips the push, and the condition runs again. i becomes 4 and 5, and both are stored. The condition 5 < 5 fails. kept is [1, 2, 4, 5].
A do...while loop has the same rule. continue skips the rest of the body and jumps to the condition at the bottom. The counter still has to change before that continue, or the condition stays true.
let n = 0;
const seen = [];
do {
n++;
if (n === 2) {
continue;
}
seen.push(n);
} while (n < 4);
The body runs once before the condition. n becomes 1 and is stored. n becomes 2 and continue skips the push, then n < 4 is tested. n becomes 3 and is stored. n becomes 4 and is stored. 4 < 4 is false. seen is [1, 3, 4].
In a for...of loop, continue moves to the next value from the iterable. This loop keeps the active users:
const users = [
{ name: "ada", active: true },
{ name: "bea", active: false },
{ name: "cy", active: true },
];
const active = [];
for (const user of users) {
if (!user.active) {
continue;
}
active.push(user.name);
}
The first object has active: true, so "ada" is pushed. The second has active: false, so continue skips the push. The third stores "cy". active is ["ada", "cy"]. The same statement works for any iterable, including a string, a Set, and a Map. In a for await...of loop, continue moves on to the next awaited value.
In a for...in loop, continue moves to the next enumerable property. It does not skip inherited names by itself. Object.hasOwn does that check:
const proto = { inherited: true };
const account = Object.create(proto);
account.name = "ada";
account.email = "";
const fields = [];
for (const key in account) {
if (!Object.hasOwn(account, key)) {
continue;
}
if (account[key] === "") {
continue;
}
fields.push(key);
}
for...in visits name, email, and the inherited key inherited. Object.hasOwn(account, "inherited") is false, so that key is skipped. account.email is "", so email is skipped. fields is ["name"].
A labeled continue
An unlabeled continue affects only the innermost loop around it. A label on an outer loop lets continue target that outer loop. You write the label as an identifier and a colon directly in front of the loop: rowLoop: for (...). The continue has to be nested inside that labeled statement, and the labeled statement has to be a loop.
const grid = [
[1, 2],
[0, 9],
[3, 4],
];
const kept = [];
rowLoop: for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
if (grid[r][c] === 0) {
continue rowLoop;
}
}
kept.push(grid[r]);
}
r starts at 0. The inner loop sees 1 and 2. Neither is 0, so the inner loop finishes and kept receives [1, 2]. The outer update runs and r is 1. The inner loop then reads 0. continue rowLoop skips kept.push(grid[r]) and the rest of that outer iteration. The outer update still runs, so r becomes 2. The last row is 3 and 4, and kept receives [3, 4]. r becomes 3 and the outer condition fails. kept is [[1, 2], [3, 4]]. The row [0, 9] is not in kept.
Drop the label and the push still runs for that row. continue then only ends the current inner iteration, and the inner body has nothing after the if:
const grid = [
[1, 2],
[0, 9],
[3, 4],
];
const kept = [];
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[r].length; c++) {
if (grid[r][c] === 0) {
continue;
}
}
kept.push(grid[r]);
}
Every row is pushed, including [0, 9]. kept is [[1, 2], [0, 9], [3, 4]].
The label stays on the same line as continue. A line break is parsed as a plain continue, and the next line is a separate statement. That continue jumps to the next iteration before the next line runs.
Where continue is a syntax error
continue has to be nested, directly or indirectly, in a loop in the same function. A nested function does not count, even when that function sits inside a loop. This program does not run:
for (let i = 0; i < 3; i++) {
(() => {
continue;
})();
}
The engine reports SyntaxError: Illegal continue statement: no surrounding iteration statement. forEach calls a function for each element, so continue inside that callback is the same error:
[1, 2, 3].forEach((n) => {
if (n === 2) {
continue;
}
});
return is allowed in that callback. It leaves the callback only. forEach still calls the callback for the remaining elements, and it always returns undefined. There is no break for forEach either, short of throwing.
const nums = [];
[1, 2, 3].forEach((n) => {
if (n === 2) {
return;
}
nums.push(n);
});
The callback returns immediately for 2, so 2 is not pushed. The callback then runs for 3 and pushes it. nums is [1, 3].
return inside a for loop is a different statement. It leaves the whole function, so the loop does not continue at all:
function collect(nums) {
const kept = [];
for (const n of nums) {
if (n < 0) {
return kept;
}
if (n === 0) {
continue;
}
kept.push(n);
}
return kept;
}
collect([1, 0, 2, -1, 3]) pushes 1, skips 0, and pushes 2. The value -1 hits return kept, so the function result is [1, 2]. The value 3 is never read.
A continue at the top level of a script, a module, a function body, or a static initialization block is also a syntax error. A loop inside that function or static block can still contain one. A label used by continue has to name an enclosing loop. A label on a plain block, or a label that is not around the continue, is a syntax error.
continue and break
break leaves the loop. continue does not.
| Statement | What happens next |
|---|---|
continue | The rest of this iteration is skipped. The loop starts another iteration if its condition still passes. |
break | The loop ends. Later iterations do not run. |
const withContinue = [];
for (let i = 0; i < 5; i++) {
if (i === 2) {
continue;
}
withContinue.push(i);
}
const withBreak = [];
for (let i = 0; i < 5; i++) {
if (i === 2) {
break;
}
withBreak.push(i);
}
In the first loop, i is 2, the push is skipped, and i++ still runs. 3 and 4 are stored. withContinue is [0, 1, 3, 4]. In the second loop, break ends it when i is 2. withBreak is [0, 1].
You want continue when the other values still matter. You want break when the rest of the loop is useless, such as after a search has found its match.
continue and an if
An if around the rest of the body skips the same work. continue lets you leave the loop early in the iteration and keep the rest of the body at the left.
function greet(name) {
return "Hello, " + name;
}
const names = ["ada", "", "grace"];
const greeted = [];
for (const name of names) {
if (name === "") {
continue;
}
greeted.push(greet(name));
}
greet runs for "ada" and "grace". It does not run for "". greeted is ["Hello, ada", "Hello, grace"].
This version calls greet for the same two strings:
function greet(name) {
return "Hello, " + name;
}
const names = ["ada", "", "grace"];
const greeted = [];
for (const name of names) {
if (name !== "") {
greeted.push(greet(name));
}
}
greeted is ["Hello, ada", "Hello, grace"].
Array.prototype.filter can remove the empty string before the loop. It builds a new array, then the loop only sees the names you kept:
function greet(name) {
return "Hello, " + name;
}
const names = ["ada", "", "grace"];
const greeted = [];
for (const name of names.filter((name) => name !== "")) {
greeted.push(greet(name));
}
greeted is again ["Hello, ada", "Hello, grace"]. names is still ["ada", "", "grace"].
Put the continue at the top of the body, on a condition you can read in one line. Use a label only when you need to skip an iteration of an outer loop. In a while or do...while loop, update the counter before continue.