continue Statement

Introduction

The continue statement is a loop control statement in JavaScript that is used to skip the current iteration of a loop and immediately move to the next iteration.

Unlike the break statement, which completely terminates a loop, the continue statement only skips the remaining code for the current iteration. The loop itself continues to execute until its condition becomes false.

For automation engineers, the continue statement is useful when processing large collections of data, skipping invalid test data, ignoring hidden web elements, filtering API responses, and continuing execution without stopping the entire loop.


What is a continue Statement?

A continue statement skips the remaining statements inside the current iteration of a loop and starts the next iteration immediately.

It can be used inside:

  • for loops

  • while loops

  • do...while loops

  • for...of loops

  • for...in loops


Syntax

continue;

Flow of Execution

Start Loop
    │
    ▼
Execute Code
    │
    ▼
Continue Condition?
    │
 ┌──┴────────────┐
 │               │
Yes             No
 │               │
 ▼               ▼
Skip Current   Execute Remaining
Iteration      Statements
 │               │
 └──────► Next Iteration

Why Do We Use the continue Statement?

The continue statement helps developers:

  • Skip unwanted data.

  • Avoid unnecessary nested conditions.

  • Improve code readability.

  • Process only valid records.

  • Continue loop execution efficiently.


Basic Example

for (let i = 1; i <= 5; i++) {

    if (i === 3) {

        continue;

    }

    console.log(i);

}

Output

1
2
4
5

The number 3 is skipped.


Example with a while Loop

let count = 0;

while (count < 5) {

    count++;

    if (count === 3) {

        continue;

    }

    console.log(count);

}

Output

1
2
4
5

Notice that the loop continues after skipping 3.


Example with a do...while Loop

let number = 0;

do {

    number++;

    if (number === 3) {

        continue;

    }

    console.log(number);

} while (number < 5);

Output

1
2
4
5

Using continue with a for...of Loop

const fruits = ["Apple", "Banana", "Orange"];

for (const fruit of fruits) {

    if (fruit === "Banana") {

        continue;

    }

    console.log(fruit);

}

Output

Apple
Orange

Using continue with a for...in Loop

const employee = {

    id: 101,

    name: "John",

    salary: 50000

};

for (const key in employee) {

    if (key === "salary") {

        continue;

    }

    console.log(`${key}: ${employee[key]}`);

}

Output

id: 101
name: John

The salary property is skipped.


Real-World Example

Suppose an online shopping application skips out-of-stock products.

const stock = [10, 0, 8, 0, 15];

for (const quantity of stock) {

    if (quantity === 0) {

        continue;

    }

    console.log(`Available Quantity: ${quantity}`);

}

Output

Available Quantity: 10
Available Quantity: 8
Available Quantity: 15

Another example:

const students = ["John", "", "Alice", "David"];

for (const student of students) {

    if (student === "") {

        continue;

    }

    console.log(student);

}

Output

John
Alice
David

Automation Testing Example

Automation engineers frequently use continue statements to skip invalid or unnecessary data while processing test cases.

Playwright Example

Skip hidden buttons.

const buttons = await page.locator("button").all();

for (const button of buttons) {

    if (!(await button.isVisible())) {

        continue;

    }

    await button.click();

}

Only visible buttons are clicked.


Selenium Example

Skip empty links.

const links = await driver.findElements(By.tagName("a"));

for (const link of links) {

    const text = await link.getText();

    if (text === "") {

        continue;

    }

    console.log(text);

}

API Testing Example

Skip unsuccessful responses.

const statusCodes = [200, 404, 500, 200];

for (const code of statusCodes) {

    if (code !== 200) {

        continue;

    }

    console.log("Valid Response");

}

Output

Valid Response
Valid Response

Data-Driven Testing Example

Skip inactive users.

const users = [

    { name: "John", active: true },

    { name: "Alice", active: false },

    { name: "David", active: true }

];

for (const user of users) {

    if (!user.active) {

        continue;

    }

    console.log(`Testing ${user.name}`);

}

Output

Testing John
Testing David

break vs continue

Using break

for (let i = 1; i <= 5; i++) {

    if (i === 3) {

        break;

    }

    console.log(i);

}

Output

1
2

The loop stops completely.


Using continue

for (let i = 1; i <= 5; i++) {

    if (i === 3) {

        continue;

    }

    console.log(i);

}

Output

1
2
4
5

The loop skips only the current iteration.


Common Mistakes

Forgetting to Update the Loop Variable

Incorrect:

let i = 1;

while (i <= 5) {

    if (i === 3) {

        continue;

    }

    i++;

}

This creates an infinite loop because i is never incremented when it equals 3.

Correct:

let i = 1;

while (i <= 5) {

    i++;

    if (i === 3) {

        continue;

    }

}

Using continue Outside a Loop

Incorrect:

continue;

This causes a syntax error because continue can only be used inside loops.


Confusing break and continue

Remember:

  • break → Ends the loop.

  • continue → Skips only the current iteration.


Best Practices

Use continue to Skip Invalid Data

Instead of deeply nested if statements, use continue to ignore invalid records and keep the code clean.


Avoid Excessive Use

Too many continue statements can make the program flow harder to understand.


Update Loop Variables Properly

Especially in while and do...while loops, ensure the loop variable is updated before reaching continue.


Use Meaningful Conditions

Write clear conditions that explain why an iteration is being skipped.


Conclusion

The continue statement is a useful control statement that skips the current iteration of a loop while allowing the remaining iterations to continue. It helps simplify loop logic, improve readability, and efficiently process only the required data.

For automation engineers, the continue statement is particularly valuable for skipping invalid test data, hidden elements, unsuccessful API responses, inactive users, and other unwanted records without terminating the entire loop.


Frequently Asked Questions (FAQs)

What is a continue statement in JavaScript?

A continue statement skips the current iteration of a loop and immediately starts the next iteration.


What is the syntax of a continue statement?

continue;

Which loops support the continue statement?

The continue statement can be used with:

  • for

  • while

  • do...while

  • for...of

  • for...in


What is the difference between break and continue?

  • break terminates the loop completely.

  • continue skips only the current iteration and continues with the next one.


Why is the continue statement useful in automation testing?

Automation engineers use continue to skip invalid test data, hidden elements, failed API responses, inactive users, and unnecessary records while allowing the remaining test execution to continue.


Key Takeaways

  • The continue statement skips the current iteration of a loop.

  • It works with for, while, do...while, for...of, and for...in loops.

  • Unlike break, it does not terminate the loop.

  • It is useful for filtering unwanted data during iteration.

  • Always update loop variables in while and do...while loops.

  • Avoid excessive use of continue statements.

  • Use meaningful conditions to improve readability.

  • continue cannot be used outside loops.

  • It is widely used in JavaScript development and automation testing.

  • Mastering the continue statement helps write cleaner, more efficient, and maintainable looping logic.