JavaScript Code

JavaScript Infinite While Loop


Infinite While Loop

while (true) {
  //code
}
  • while is keyword
  • true is boolean value
  • code is any valid JavaScript code

if the condition is always true in a while loop, then the while loop runs indefinitely. therefore we have an infinite while loop

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

Examples

1. while loop to print 'hello world' infinitely

let i = 0;
while (true) {
    console.log('hello world');
    i++;
}

note: do not run this code. your browser may freeze, or raise an alert