JavaScript Continue Statement
Learn how to skip iterations in loops
⏭️ What is the Continue Statement?
The
continue
statement skips the current iteration of a loop and jumps to the next iteration. It's like saying "skip this one and move to the next!"
// Simple continue example
for (let i = 1; i <= 5; i++) {
if (i === 3) {
continue; // Skip when i equals 3
}
console.log(i);
}
// Output: 1, 2, 4, 5
Console Output:
2
4
5
Continue vs Break
Continue
Skips current iteration
for (let i = 1; i <= 3; i++) {
if (i === 2) continue;
console.log(i);
}
// Output: 1, 3
Break
Exits the entire loop
for (let i = 1; i <= 3; i++) {
if (i === 2) break;
console.log(i);
}
// Output: 1
For Loops
Skip specific iterations
for (let i = 0; i < 5; i++) {
if (i % 2 === 0) continue;
console.log(i); // odd numbers
}
While Loops
Skip based on conditions
let i = 0;
while (i < 5) {
i++;
if (i === 3) continue;
console.log(i);
}
🔹 Continue in For Loops
Use continue to skip specific iterations based on conditions:
// Print only positive numbers
let numbers = [-2, -1, 0, 1, 2, 3];
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] <= 0) {
continue; // Skip negative numbers and zero
}
console.log("Positive number:", numbers[i]);
}
Console Output:
Positive number: 2
Positive number: 3
🔹 Continue with Arrays
Skip processing certain array elements:
// Process only valid email addresses
let emails = ["[email protected]", "invalid-email", "[email protected]", "", "[email protected]"];
for (let email of emails) {
// Skip empty strings or emails without @
if (!email || !email.includes("@")) {
console.log(`Skipping invalid: "${email}"`);
continue;
}
console.log(`Processing: ${email}`);
// Email processing logic would go here
}
Console Output:
Skipping invalid: "invalid-email"
Processing: [email protected]
Skipping invalid: ""
Processing: [email protected]
🔹 Continue in While Loops
Be careful with continue in while loops - make sure to update the counter:
// Print even numbers from 1 to 10
let num = 0;
while (num < 10) {
num++; // IMPORTANT: Increment before continue
if (num % 2 !== 0) {
continue; // Skip odd numbers
}
console.log(`Even number: ${num}`);
}
Console Output:
Even number: 4
Even number: 6
Even number: 8
Even number: 10
🔹 Practical Example: Data Filtering
Use continue to filter and process data:
// Process student grades, skip invalid entries
let students = [
{ name: "Alice", grade: 85 },
{ name: "", grade: 90 }, // Invalid: no name
{ name: "Bob", grade: -5 }, // Invalid: negative grade
{ name: "Charlie", grade: 92 },
{ name: "Diana", grade: 150 } // Invalid: grade too high
];
console.log("Valid student grades:");
for (let student of students) {
// Skip students with invalid data
if (!student.name || student.grade < 0 || student.grade > 100) {
console.log(`Skipping invalid entry: ${JSON.stringify(student)}`);
continue;
}
// Process valid student
let letterGrade = student.grade >= 90 ? 'A' :
student.grade >= 80 ? 'B' :
student.grade >= 70 ? 'C' : 'D';
console.log(`${student.name}: ${student.grade} (${letterGrade})`);
}
Console Output:
Alice: 85 (B)
Skipping invalid entry: {"name":"","grade":90}
Skipping invalid entry: {"name":"Bob","grade":-5}
Charlie: 92 (A)
Skipping invalid entry: {"name":"Diana","grade":150}