JS Break and Continue

HTML
CSS
C#
SQL

Break and Continue Statement

Break Statement:

The `break` statement in JavaScript is used to terminate a loop prematurely. When encountered, it immediately exits the loop, and the program continues with the next statement after the loop.

Example:

In this example,

The loop will print numbers 1 and 2, then encounter the `break` statement when `i` is 3, and exit the loop.

Continue Statement:

The `continue` statement in JavaScript is used to skip the current iteration of a loop and move on to the next one.

Example:

for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue; // skips iteration when i is 3
  }
  console.log(i);
}

In this example, the loop will print numbers 1, 2, 4, and 5. When `i` is 3, the `continue` statement is encountered, and the loop skips the rest of the code for that iteration.

The `break` and `continue` statements are useful for controlling the flow of loops and handling specific conditions during their execution.

Course Video