JavaScript Code


JavaScript Infinite For Loop


Infinite For Loop

for (;true;) {
  //code
}

where

  • for is keyword
  • true is boolean value
  • code is any valid JavaScript code

If the condition is always true in a For loop, then the for loop runs indefinitely. Therefore we have an infinite for loop.

Instead of true for the condition, we can also provide a boolean expression that always evaluates to true, like 1 == 1.

Examples

1. For loop to print 'hello world' infinitely.

for (;true;) {
    console.log('hello world');
}

Note: Do not run this code. your browser may freeze, or raise an alert.