
In JavaScript, an infinite loop is a loop that never terminates unless the browser or environment forcibly stops it. There are multiple ways to create an infinite loop. Here’s an example using a while
loop:
JavaScript
while (true) {
// Code to run indefinitely
console.log("This will run forever!");
}
Alternatively, you can create an infinite loop using a for
loop:
JavaScript
for (;;) {
// Code to run indefinitely
console.log("This will also run forever!");
}
In both cases, the condition always evaluates to true
, and the loops will continue to execute endlessly.