4.6 Infinite Loops
When you write loops, you absolutely need to know what an infinite loop is. As the name suggests, an infinite loop runs forever, and if you create one by accident it will freeze your browser or even your computer. Let's walk through a few classic ways to fall into that trap so you can recognise them in your own code.
Start with a while loop using a counter:
let i = 0;
while (i < 5) {
console.log(i);
// Forgot: i++;
}
Technically, we must increment i inside the body. If we forget, on the first iteration i is 0, less than 5, so we log it. On the second iteration i is still 0 and still less than 5, and the loop keeps going. If you save the changes, the console fills up with 0s at growing speed and the browser tab hangs. The only way out is to close the tab, or force-close it if it stops responding.
Other shapes of the same trap
while (true) { ... }— the condition is always true, so the loop never stops.do { ... } while (true);— same problem with ado...while.for (let i = 0; i > 0; i++) { ... }— wrong condition or, in many real cases, a correct condition likei < 10but no increment in the third part of theforheader.
Infinite loops are not limited to while. They show up in do...while and for just as easily, usually because we forgot to update the loop variable or because the condition can never become false. Be aware of them and avoid them, because they can crash your browser or your computer. That's it for this short demonstration on infinite loops — see you in the next video, where we'll cover for...in loops.
Summary
Infinite loops are loops that never terminate because the exit condition is never met. This lesson covers how infinite loops happen (forgetting to increment loop variables), their consequences (crashing browsers and systems), and examples across different loop types (while, do-while, for).
Key points
- Infinite loops occur when the exit condition is never satisfied, causing the loop to run forever
- Forgetting to increment the loop variable (e.g., i++) is a common cause of infinite loops
- Infinite loops can crash your browser or computer due to continuous resource consumption
- Infinite loops are not limited to while loops—they can occur in do-while and for loops as well
- To stop an infinite loop, you must close the application or force-quit it
- Always ensure loop increment statements (third part of a for loop) are present to avoid infinite loops
FAQ
What happens if I accidentally create an infinite loop?
An infinite loop runs forever and consumes system resources, causing your browser or computer to freeze or crash. You'll need to close the application or force-quit it to stop it.
What are common mistakes that lead to infinite loops?
The most common mistake is forgetting to increment the loop variable (e.g., i++ in a while loop, or the increment expression in a for loop). Without this, the exit condition is never reached.
Can infinite loops happen in all types of loops?
Yes, infinite loops can occur in while loops, do-while loops, and for loops—whenever the condition that should terminate the loop is never satisfied.