Infinite While Loop
while (true) {
//code
}
where
while
is keywordtrue
is boolean valuecode
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'
indefinitely.
let i = 0;
while (true) {
console.log('hello world');
i++;
}
Note: Do not run this code. your browser may freeze, or raise an alert.