**continue** jumps to the next iteration of the loop, skipping the remaining code in the current iteration. Unlike **break**, it does not exit the loop. It's useful for filtering items inside a loop without deeply nesting if statements.
1Understanding continue Statement
continue jumps to the next iteration of the loop, skipping the remaining code in the current iteration. Unlike break, it does not exit the loop. It's useful for filtering items inside a loop without deeply nesting if statements.
continue reduces nesting by inverting conditions: instead of 'if (valid) { doWork() }', write 'if (!valid) continue; doWork()'.
// Process only positive numbers
const nums = [1, -2, 3, -4, 5, -6];
const positives = [];
for (const n of nums) {
if (n <= 0) continue; // skip negatives
positives.push(n * 2); // only runs for positives
}
console.log(positives);2Practical Example
Here is a real-world application of continue Statement showing how it is used in production JavaScript code.
// Skip processing errors in a batch
const records = [
{ id: 1, valid: true },
{ id: 2, valid: false },
{ id: 3, valid: true },
];
for (const rec of records) {
if (!rec.valid) { console.log(`Skipping ${rec.id}`); continue; }
console.log(`Processing ${rec.id}`);
}3Best Practices
Follow these guidelines when working with continue Statement:
1. Use continue to skip invalid items early in the loop
2. Combine with labeled statements for nested loops
3. Consider filter() as a cleaner alternative for arrays
Tip: continue reduces nesting by inverting conditions: instead of 'if (valid) { doWork() }', write 'if (!valid) continue; doWork()'.
// Process only positive numbers
const nums = [1, -2, 3, -4, 5, -6];
const positives = [];
for (const n of nums) {
if (n <= 0) continue; // skip negatives
positives.push(n * 2); // only runs for positives
}
console.log(positives);