JavaScript Loop Control Statements Tutorial

4/14/2025

#JavaScript Loop Control Statements Turial

Go Back
#JavaScript Loop Control Statements Turial

JavaScript Loop Control Statements Tutorial

Learn how to control the flow of loops in JavaScript using break, continue, and labels.

Loop control statements in JavaScript help manage the execution flow within loops. These tools are essential for writing efficient and readable code.


📄 What are Loop Control Statements?

Loop control statements allow you to:

  • Exit a loop early

  • Skip specific iterations

  • Control nested loops using labels

JavaScript provides three main loop control tools:

  1. break

  2. continue

  3. label (used with break and continue)


🔹 break Statement

The break statement exits the loop immediately, regardless of the condition.

Example:

for (let i = 0; i < 10; i++) {
  if (i === 5) break;
  console.log(i);
}
// Output: 0 1 2 3 4

🔹 continue Statement

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

Example:

for (let i = 0; i < 5; i++) {
  if (i === 2) continue;
  console.log(i);
}
// Output: 0 1 3 4

🔹 Labels with break and continue

Labels allow you to control nested loops more precisely.

Example:

outerLoop:
for (let i = 0; i < 3; i++) {
  for (let j = 0; j < 3; j++) {
    if (i === j) break outerLoop;
    console.log(`i: ${i}, j: ${j}`);
  }
}

✅ When to Use Loop Control Statements

  • Use break to terminate a loop early when a condition is met.

  • Use continue to skip unnecessary iterations.

  • Use labels when dealing with nested loops that need complex control.


🏪 Best Practices

  • Avoid overusing labels; they can make code harder to follow.

  • Prefer simpler loop logic when possible.

  • Always comment your code when using nested control statements.


🎓 Conclusion

Loop control statements like break, continue, and labels provide essential tools for controlling flow in JavaScript loops. Mastering these helps you write efficient, bug-free code.

Table of content