Javascript Loop Control Statements

Javascript Loop Control Statements

  • break: Exits the loop immediately.
  • continue: Skips the current iteration and proceeds to the next iteration.

Loop Control Statements

1. break

The break statement exits the loop when a specified condition is met.

Example: Exit the loop when i equals 3

  for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        break; // Exits the loop
    }
    console.log(i);
  }

Output: 1, 2


2. continue

The continue statement skips the current iteration and proceeds to the next one.

Example: Skip printing when i equals 3

  for (let i = 1; i <= 5; i++) {
    if (i === 3) {
        continue; // Skips the rest of the loop for this iteration
    }
    console.log(i);
  }

Output: 1, 2, 4, 5


Previous Next