JavaScript infinite loop: forever loop syntax, bugs, and how to avoid them

javascript infinite loop, infinite loop javascript, infinite loop in javascript, infinite loop js, js infinite loop, forever loop javascript, javascript endless loop, how to make an infinite loop in javascript, javascript forever loop, infinite loop in js: while true, for (;;), missing increment, break.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript infinite loop: forever loop syntax, bugs, and how to avoid them

An infinite loop is any while, for, or recursive pattern whose exit condition never becomes true, so execution never reaches the code below it. That is useful to recognize because it can freeze a browser tab or block a Node process on the main thread. Many “run forever” workloads—polling, animation, background work—are better expressed with setInterval, requestAnimationFrame, or async scheduling primitives; MDN’s Loops and iteration guide is a good companion read.

A true infinite loop never reaches the line after it; the main thread stays busy, so the browser tab or Node process can freeze. The runnable samples below use the same syntax as endless loops but add a break or a guard so automated runs always finish. Treat them as syntax demos, not production patterns.

Tested on: Node.js v20.18.2. A short note after each runnable snippet describes what you should see in the console.


while (true) forever loop (bounded demo)

javascript
let count = 0;
while (true) {
  console.log(1);
  if (++count >= 3) break;
}
Output

You should see 3 lines, in order: 1, 1, 1.

Without break, return, throw, or an asynchronous suspension, this would print forever.

Diagram: contrast between an unguarded infinite while true loop and a safe pattern using break or a counter guard


while with a condition that never progresses

If the body never moves i toward the exit test, i < 10 stays true forever. Here a guard stops the demo after three prints:

javascript
let i = 1;
let guard = 0;
while (i < 10) {
  console.log(i);
  if (++guard >= 3) break;
}
Output

You should see 3 lines, in order: 1, 1, 1.

The fix is to mutate i (or another driving value) so the condition can eventually fail.


for (;;) “for ever” syntax

C-style infinite for omits init, test, and update. Again, a break ends the sample:

javascript
let n = 0;
for (;;) {
  console.log(n);
  if (++n >= 4) break;
}
Output

You should see 4 lines, in order: 0, 1, 2, 3.


for with a literal true test

javascript
let index = 0;
for (let j = 0; true; j++) {
  console.log(j);
  if (++index >= 4) break;
}
Output

You should see 4 lines, in order: 0, 1, 2, 3.

The update clause (j++ here) still runs, but you still need a real stop condition unless you intentionally build a worker loop around break / async waits.


Healthy terminating for loop

javascript
for (let i = 0; i < 10; i++) {
  console.log(i);
}
Output

You should see 10 lines, in order: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

Every part matters: initializer, condition, and update.


Missing update clause (bounded demo)

javascript
let i = 0;
let guard = 0;
for (; i < 10; ) {
  console.log(i);
  if (++guard >= 3) break;
}
Output

You should see 3 lines, in order: 0, 0, 0.

Without the guard, i never changes, so the loop would never exit.


Stopping a runaway loop in the browser or Node

Illustration: use DevTools pause, close the tab, or in Node send Ctrl+C to interrupt a stuck loop

Chrome, Edge, and Firefox let you open DevTools and pause script execution, or you can close the tab entirely. In Node, press Ctrl+C in the terminal where the process runs. Prevention still beats recovery: keep loop progress obvious, add temporary maximum-iteration guards while debugging, and prefer scheduling APIs such as setInterval or setTimeout for recurring work instead of busy-waiting.


Summary

Javascript infinite loop searches mix teaching examples with real incidents: while (true) or a missing update clause can stall the event loop in browsers or freeze a Node process until you interrupt it. The practical FAQ is how to stop one—DevTools pause, tab close, or Ctrl+C in Node—and how to avoid accidental loops by always advancing the induction variable or using break guards while prototyping.

For intentional polling, prefer setInterval, requestAnimationFrame, or async iterators so work yields to the host; busy-waiting on Date.now() is almost never the right pattern in JavaScript. Pair this article with structured logging when you diagnose rare hangs in production, because the root cause is often network or database waits rather than a literal while (true).


References

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …