The continue statement in JavaScript is used within loops to skip the current iteration and move directly to the next one.
This is useful when certain conditions are met, and you want to bypass the rest of the code in the loop for that iteration.
The continue statement can be used in any of the loop structures: for, while, or do…while.
Syntax of continue
continue;
When the continue statement is encountered, the loop immediately jumps to the next iteration.
1. Using continue in a for Loop
The continue statement is often used in for loops to skip certain iterations.
Example 1: Skipping Even Numbers
for (let i = 1; i <= 10; i++) { // If the number is even, skip this iteration if (i % 2 === 0) { continue; } console.log(i); }
Output
1 3 5 7 9
Explanation
The loop iterates from 1 to 10.
The if condition checks if the current number i is even (i % 2 === 0). If true, continue is executed, skipping the rest of the loop body for that iteration.
As a result, only odd numbers are printed to the console.
Example 2: Using continue to Skip Specific Values
for (let i = 1; i <= 5; i++) { if (i === 3) { continue; // Skip the iteration when i is 3 } console.log(i); } Output
1 2 4 5
Explanation
When i is 3, the continue statement is executed, skipping the console.log(i) statement.
The loop continues with the next iteration, effectively skipping the number 3.
2. Using continue in a while Loop
The continue statement can also be used inside a while loop to skip to the next iteration.
Example 3: Skipping Negative Numbers
let numbers = [1, -2, 3, -4, 5]; let index = 0; while (index < numbers.length) { if (numbers[index] < 0) { index++; continue; // Skip the rest of the loop for negative numbers } console.log(numbers[index]); index++; }
Output
1 3 5
Explanation
The while loop iterates through the numbers array.
If the current element (numbers[index]) is negative, the continue statement is executed.
This skips the console.log(numbers[index]) statement and moves to the next iteration.
3. Using continue in a do…while Loop
The continue statement also works with do…while loops, allowing you to skip the current iteration.
Example 4: Skipping Certain Values in a do…while Loop
let i = 0; do { i++; if (i === 4) { continue; // Skip the iteration when i is 4 } console.log(i); } while (i < 6);
Output
1 2 3 5 6
Explanation
The do…while loop increments i and checks if i is 4.
When i is 4, the continue statement is executed, skipping the console.log(i) statement.
The loop continues to the next iteration without printing 4.
4. Using continue in Nested Loops
In nested loops, the continue statement only affects the innermost loop in which it is placed.
Example 5: Using continue in Nested Loops
for (let i = 1; i <= 3; i++) { for (let j = 1; j <= 3; j++) { if (j === 2) { continue; // Skip the iteration when j is 2 } console.log(`i = ${i}, j = ${j}`); } }
Output
i = 1, j = 1 i = 1, j = 3 i = 2, j = 1 i = 2, j = 3 i = 3, j = 1 i = 3, j = 3
Explanation
The continue statement is inside the inner loop (for j).
When j is 2, the continue statement is executed, skipping the console.log statement for that iteration of the inner loop.
The outer loop (for i) is not affected by the continue statement in the inner loop.
5. Skipping Over Certain Conditions with continue
Example 6: Skipping Over Non-Numeric Values in an Array
let items = [1, 'two', 3, 'four', 5]; for (let i = 0; i < items.length; i++) { if (typeof items[i] !== 'number') { continue; // Skip non-numeric values } console.log(items[i]); }
Output
1 3 5
Explanation
The loop iterates over the items array.
The if statement checks if the current element is not a number. If true, the continue statement skips to the next iteration.
Only numeric values are printed to the console.
Summary
The continue statement is a handy tool for controlling the flow of loops in JavaScript. It allows you to skip the current iteration and move on to the next, which can be useful in various scenarios like filtering data, handling specific conditions, or controlling nested loops.
Key Points
The continue statement skips the remaining code in the current iteration and moves to the next iteration of the loop.
Works with all types of loops: for, while, and do…while.
In nested loops, continue affects only the innermost loop where it is used.
Useful for skipping unwanted iterations based on specific conditions.
Use Cases
Filtering: Skip specific elements in an array (e.g., non-numeric values, negative numbers).
Conditional Skipping: Control which iterations are processed in complex loops (e.g., nested loops).
Efficient Processing: Quickly skip over iterations that do not meet certain conditions.
By understanding how to use the continue statement effectively, you can write more efficient and readable loop structures in your JavaScript programs.