JavaScript continue
Statement
In JavaScript, sometimes you may want to skip the rest of the current iteration of a loop and proceed directly to the next iteration. This is where the continue
statement comes in handy. It allows you to bypass the remaining code inside the loop for the current iteration and move to the next one.
continue
Statement in JavaScript?The continue
statement is used inside loops to skip the current iteration and move to the next one. When the continue
statement is encountered, it immediately ends the current iteration and proceeds with the next cycle of the loop.
The continue
statement can be used in all loop structures such as for
, while
, and do...while
.
continue
Statement:
continue;
continue
in a for
LoopThe continue
statement is often used in a for
loop when you need to skip specific iterations based on a condition.
continue
in a for
Loop
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue; // Skip the rest of the code when i is even
}
console.log(i); // This code runs only when i is odd
}
Explanation:
i = 0
to i = 9
.i
is even, the continue
statement skips the remaining code inside the loop, so console.log(i)
is not executed.
1
3
5
7
9
Here, the continue
statement ensures that only odd numbers are printed.
continue
in a while
LoopYou can also use the continue
statement in a while
loop to skip specific iterations and continue to the next iteration of the loop.
continue
in a while
Loop
let i = 0;
while (i < 10) {
i++;
if (i % 2 === 0) {
continue; // Skip the rest of the loop when i is even
}
console.log(i); // This code runs only when i is odd
}
Explanation:
while
loop runs as long as i
is less than 10.continue
statement skips the console.log(i)
statement when i
is even.
1
3
5
7
9
As in the previous example, only the odd numbers are printed because the continue
statement skips even numbers.
continue
in a do...while
LoopThe continue
statement can also be used in a do...while
loop to skip the rest of the iteration and proceed to the next iteration of the loop.
continue
in a do...while
Loop
let i = 0;
do {
i++;
if (i % 2 === 0) {
continue; // Skip the rest of the loop when i is even
}
console.log(i); // This code runs only when i is odd
} while (i < 10);
Explanation:
do...while
loop ensures that the loop runs at least once before checking the condition (i < 10
).continue
statement skips the console.log(i)
statement when i
is even.
1
3
5
7
9
As in the other loop examples, only odd numbers are printed because the continue
statement skips even numbers.
continue
Imagine you are processing user input and want to skip invalid entries, such as empty or incorrect values. The continue
statement is a great way to skip invalid inputs without breaking the entire loop.
let inputs = ["", "hello", "123", "world", " ", "JavaScript"];
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].trim() === "") {
continue; // Skip empty or whitespace strings
}
console.log("Valid input: " + inputs[i]);
}
Explanation:
continue
statement skips over the empty or whitespace-only strings in the inputs
array.
Valid input: hello
Valid input: 123
Valid input: world
Valid input: JavaScript
In this example, the continue
statement helps filter out invalid entries and ensures that only valid inputs are processed.
continue
with LabelsJust like the break
statement, you can use labeled continue
to skip the current iteration of an outer loop from within a nested loop.
continue
to Skip an Outer Loop
outerLoop:
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (i === 1 && j === 1) {
continue outerLoop; // Skip to the next iteration of the outer loop
}
console.log("i = " + i + ", j = " + j);
}
}
Explanation:
continue outerLoop
statement skips the inner loop entirely when i === 1
and j === 1
, and moves to the next iteration of the outer loop.
i = 0, j = 0
i = 0, j = 1
i = 0, j = 2
i = 1, j = 0
i = 1, j = 2
i = 2, j = 0
i = 2, j = 1
i = 2, j = 2
The continue outerLoop
statement helps control the flow of the outer loop from within the inner loop