Skip an Iteration
continue skips the rest of the current loop iteration and moves to the next one.
It is useful for filtering values without deeply nested conditions.
JavaScript Tutorial
The continue statement skips the current iteration and jumps to the next one.
It helps filter items without deep nesting.
Filtering values is common when processing arrays or streams.
continue keeps the loop body clean and focused on valid items.
continue;for (let i = 0; i < 5; i++) {
if (i === 2) continue;
console.log(i);
}The value 2 is skipped.
const nums = [1, 2, 3, 4, 5];
for (const n of nums) {
if (n % 2 !== 0) continue;
console.log(n);
}continue skips all odd numbers.
continue skips the rest of the current loop iteration and moves to the next one.
It is useful for filtering values without deeply nested conditions.
continue makes it easy to ignore certain items while looping.
It keeps the main logic flatter and easier to read.
Use continue alongside guard checks to avoid nested if blocks.
Be careful to still update counters to avoid infinite loops.
let i = 0;
while (i < 5) {
i++;
if (i === 3) continue;
console.log(i);
}Remember to update the counter before continuing.
const items = ["a", "", "b"];
for (const item of items) {
if (!item) continue;
console.log(item);
}Skip empty strings with a continue guard.
Without
for (const n of nums) {
if (n % 2 === 0) {
console.log(n);
}
}With
for (const n of nums) {
if (n % 2 !== 0) continue;
console.log(n);
}Make sure the loop state still changes before continue.
Use clear conditions; too many continues can be confusing.
Ensure skipped iterations do not miss required side effects.
It skips the rest of the current loop iteration.
When filtering items without nested if blocks.
Update counters before continue in while loops.
Practice: Print only the positive numbers from an array using continue.
const nums = [-1, 2, 0, 3, -5];
// TODO: print only positives
One Possible Solution
const nums = [-1, 2, 0, 3, -5];
for (const n of nums) {
if (n <= 0) continue;
console.log(n);
}It skips the rest of the current iteration and moves to the next one.
Break exits the loop entirely; continue only skips one iteration.
Yes, but make sure the loop state still changes.
Try skipping different values and see how the output changes.