Skipping with continue in JavaScript
📍 What is continue in JavaScript?
The continue statement is used to skip the current iteration of a loop and proceed with the next one. It's helpful when you want to ignore certain values or conditions without stopping the entire loop. 🔄
>>“Sometimes the best move is to just skip and continue.” ⏩
🧪 Syntax
Basic continue Syntax
continue;That's it! A single statement that tells JavaScript to skip the rest of the loop body for the current iteration.
🔁 Using continue in a Loop
Skip even numbers
for (let i = 1; i <= 5; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}Output: 1, 3, 5
The loop skips printing even numbers by using continue.
Note
💡 continue only skips the current iteration — the loop doesn't stop entirely like with break.
🔄 With while Loop
continue in while loop
let i = 0;
while (i < 5) {
i++;
if (i === 3) {
continue;
}
console.log(i);
}Output: 1, 2, 4, 5
The number 3 is skipped.
📦 Real-World Use Case
You can use continue to skip invalid or unwanted items while processing an array:
Skip null or empty values
const names = ["Alice", "", null, "Bob"];
for (let i = 0; i < names.length; i++) {
if (!names[i]) {
continue;
}
console.log("✅", names[i]);
}Output:
✅ Alice
✅ Bob
⚖️ break vs. continue
| Feature | break | continue |
|---|---|---|
| Stops the loop | ✅ | ❌ |
| Skips to next iteration | ❌ | ✅ |
| Use in switch? | ✅ | ❌ |
✅ Summary
- continue skips the current iteration of a loop.
- Best used to avoid certain values or cases during iteration.
- Unlike break, it doesn’t exit the loop entirely.
📚 Further Reading
>>“Skip what doesn’t matter — and keep moving forward.” 🚀