06. Mastering JavaScript Loops: for, while, break & continue
Introduction
Loops allow you to execute a block of code repeatedly, making them indispensable for tasks like iteration, repetition, and data processing. In JavaScript, you have several loop structures (for, while, do…while) along with break and continue statements to control the flow. In this post, you’ll learn:
- How for and while loops work
- When to use each loop type
- How break and continue modify loop behavior
1. JavaScript Loop Basics
A loop repeatedly executes a block of code as long as a condition is true (or until a termination criterion). Loops help avoid redundant code and elegantly handle collections or repeated operations.
Common loop types in JavaScript:
- for loop
- while loop
- do…while loop (not covered in your links, but related)
Let’s explore for and while.
2. for Loop
The for loop is one of the most commonly used loops because it compactly integrates initialization, condition check, and iteration update in one line.
Syntax:
- initialization — executed once before the loop starts
- condition — checked before each iteration; if true, the loop body runs
- increment / update — executed after each iteration
Example:
This prints:
Because when i becomes 5, i < 5 is false, so the loop stops.
for loops are ideal when you know in advance how many iterations you want (e.g. iterating over array indices).
3. while Loop
A while loop runs as long as its condition remains true. It’s more flexible but also riskier, because if you don’t update the condition properly, you may create an infinite loop.
Syntax:
Example:
Again, outputs 0 through 4. You must ensure i is incremented (or the condition changes) inside the loop to avoid endless looping.
Use while when you don’t know exactly how many times the loop will need to run (e.g. waiting for user input or a condition to become false).
4. break Statement
break immediately exits the loop it is in, preventing any further iterations—even if the loop’s condition would allow them.
Example:
This prints 0, 1, 2, 3, 4 and stops, because i === 5 triggers break.
break is useful when a certain condition makes further looping unnecessary.
5. continue Statement
continue skips the current iteration and moves on to the next one, without exiting the loop entirely.
Example:
This prints odd numbers: 1, 3, 5, 7, 9, because when i is even, continue skips the console.log() and proceeds to the next iteration.
continue helps skip undesirable cases without needing nested conditionals.
6. Combining Loops & Control Flow
You can use break and continue in any loop type (for, while, do…while). Use them judiciously to make code cleaner:
This prints: 0, 1, 2, 4, 5, 6 and stops when i becomes 7.
Be careful: overusing break / continue inside deeply nested loops may make code hard to read.
Best Practices & Tips
- Always ensure loop conditions will eventually become false (to avoid infinite loops).
- Prefer for loops when iteration count is known; use while when it is not.
- Use break only when exiting the loop early is logically needed, not as a crutch for poor loop design.
- Use continue to simplify conditions inside loops, rather than nested if statements.
- Keep loop bodies small, and refactor repeated logic into functions when possible.
Conclusion
Loops are powerful tools in JavaScript. for and while let you repeat code, and break/continue let you control the flow flexibly. Once you're comfortable with loops, the next step is combining them with functions, arrays, and iterators to tackle more complex tasks.
06. Mastering JavaScript Loops for while break continue
coldshadow44 on 2025-10-12
(0)