JavaScript continue Statement
The JavaScript continue statement is used to skip the current iteration of a loop and proceed to the next one. It's useful when you want to bypass part of a loop's body under certain conditions, but keep the loop running.
Common use cases for the continue statement include:
- Skipping specific values: Avoid processing certain values in an array or string.
- Filtering input: Continue looping while ignoring values that don’t meet a condition.
What You'll Learn
In this tutorial, you'll explore the behavior of the continue statement in JavaScript, how it interacts with different loops, and when it's most effective.
Understanding the continue Statement
The continue statement in JavaScript is used to skip the rest of the code inside the current loop iteration and jump directly to the next iteration. Unlike break, which completely exits a loop, continue allows the loop to keep running while ignoring specific conditions.
// Example of continue statement with a list
const numbers = [1, 2, 3, 4, 5, 6, 7];
for (let number of numbers) {
if (number % 2 === 0) {
continue; // Skip even numbers
}
console.log(number); // This will print odd numbers only
}
// Example of continue statement with a list
const numbers = [1, 2, 3, 4, 5, 6, 7];
for (let number of numbers) {
if (number % 2 === 0) {
continue; // Skip even numbers
}
console.log(number); // This will print odd numbers only
}
In the example above, the loop prints only the odd numbers. When an even number is encountered, the continue statement is executed, which skips the console.log call for that iteration.
Output:
1
3
5
7
1
3
5
7
Example 1: Skipping even numbers with a for loop
You can use continue in a for loop to skip certain iterations based on a condition. Here's an example:
// Skipping even numbers
const numbers = [1, 2, 3, 4, 5, 6];
for (let number of numbers) {
if (number % 2 === 0) {
continue; // Skip even numbers
}
console.log(number); // Print odd numbers only
}
// Skipping even numbers
const numbers = [1, 2, 3, 4, 5, 6];
for (let number of numbers) {
if (number % 2 === 0) {
continue; // Skip even numbers
}
console.log(number); // Print odd numbers only
}
How It Works:
- for (let number of numbers) {: This starts a loop that goes through each item in the numbers array.
- if (number % 2 === 0): The loop checks if the number is even.
- continue: Skips the current iteration when the condition is true (i.e., for even numbers).
- So, the loop prints only the odd numbers in the array.
Output
1
3
5
1
3
5
Example 2: Skipping negative numbers with a while loop
You can also use continue in a while loop to skip negative numbers and only print positive ones. Here's an example:
// Skipping negative numbers
let i = -5;
while (i <= 5) {
if (i < 0) {
i++; // Increment first to avoid infinite loop
continue; // Skip negative numbers
}
console.log(i);
i++;
}
// Skipping negative numbers
let i = -5;
while (i <= 5) {
if (i < 0) {
i++; // Increment first to avoid infinite loop
continue; // Skip negative numbers
}
console.log(i);
i++;
}
How It Works:
- while (i <= 5): This starts a loop that runs while i is less than or equal to 5.
- if (i < 0): The loop checks if i is a negative number.
- continue: Skips the rest of the loop body if i is negative, so console.log(i) won't run.
- Only non-negative numbers (0 to 5) are printed to the console.
Output
0
1
2
3
4
5
0
1
2
3
4
5
Example 3: Skipping vowels in a string with a for loop
You can use continue to skip over vowels while iterating through a string. Here's an example:
// Skipping vowels in a string
const word = "programming";
for (let letter of word) {
if ('aeiou'.includes(letter)) {
continue; // Skip vowels
}
console.log(letter); // Print consonants only
}
// Skipping vowels in a string
const word = "programming";
for (let letter of word) {
if ('aeiou'.includes(letter)) {
continue; // Skip vowels
}
console.log(letter); // Print consonants only
}
How It Works:
- for (let letter of word): This starts a loop that iterates through each character in the word "programming".
- if ('aeiou'.includes(letter)): The loop checks if the current letter is a vowel.
- continue: Skips the current iteration if the letter is a vowel.
- The loop prints only the consonants in the string.
Output
prgrmmng
prgrmmng
Exercises
Practice the continue statement with the following exercises:
1. Print numbers from 1 to 10, skipping 5 and 7.
for (let i = 1; i <= 10; i++) {
if (i === 5 || i === 7) continue;
console.log(i);
}
for (let i = 1; i <= 10; i++) {
if (i === 5 || i === 7) continue;
console.log(i);
}
2. Prompt users for numbers, skip if divisible by 3.
while (true) {
let input = prompt("Enter a number (or type 'exit' to quit):");
if (input === "exit") break;
const num = Number(input);
if (num % 3 === 0) continue;
console.log("You entered:", num);
}
while (true) {
let input = prompt("Enter a number (or type 'exit' to quit):");
if (input === "exit") break;
const num = Number(input);
if (num % 3 === 0) continue;
console.log("You entered:", num);
}
3. Skip the letter "a" while printing a string.
const text = "banana";
for (let ch of text) {
if (ch === 'a') continue;
console.log(ch);
}
const text = "banana";
for (let ch of text) {
if (ch === 'a') continue;
console.log(ch);
}
*Tip: Try changing the conditions and values to see how continue changes the loop's behavior!
Frequently Asked Questions
What is the continue statement in JavaScript?
What is the continue statement in JavaScript?
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.
When should I use the continue statement in a loop?
When should I use the continue statement in a loop?
You should use the continue statement when you want to skip the current iteration of the loop and continue with the next one, typically when a certain condition is met.
Can I use continue inside a switch statement?
Can I use continue inside a switch statement?
No, the continue statement cannot be used directly inside a switch statement. It is only valid in loops like for, while, and do...while.
What happens if I use continue in a loop without a condition?
What happens if I use continue in a loop without a condition?
If you use the continue statement in a loop without a condition, it will simply skip to the next iteration, potentially causing an infinite loop if not used with proper conditions.
Can the continue statement be used in a nested loop?
Can the continue statement be used in a nested loop?
Yes, the continue statement can be used in a nested loop. It will skip the current iteration of the innermost loop it is placed in.
What's Next?
In the upcoming section, we'll explore the switch statement — a powerful control structure that allows you to handle multiple conditions in a clean and efficient way. The switch statement is perfect when you need to compare a variable against several potential values, making your code more readable and manageable.