HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Break and Continue

In JavaScript, the break and continue statements are used within loops to control the flow of execution.

Break Statement:

The break statement is used to exit a loop prematurely. When encountered, it immediately terminates the loop and resumes execution at the next statement after the loop.

Example

for (let i = 0; i < 5; i++) {
    if (i === 3) {
        break; // Exit the loop when i equals 3
    }
    console.log(i);
}

             
You can click on above box to edit the code and run again.

Output

Output: 0 1 2

Continue Statement:

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration. It's often used within loops to skip certain conditions or values.

Example

for (let i = 0; i < 5; i++) {
    if (i === 3) {
        continue; // Skip the current iteration when i equals 3
    }
    console.log(i);
}
You can click on above box to edit the code and run again.

Output

Output: 0 1 2 4