JavaScript Code

JavaScript Infinite For Loop


Infinite For Loop

for (;true;) {
  //code
}
  • 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