How to create a forever loop in JavaScript​

By
Last updated:

I’ll show you several ways to create an infinite loop in JavaScript:

1. Using while loop:

while (true) {
    // Code to run forever
    console.log("This will keep running");
}

2. Using for loop:

for (;;) {
    // Code to run forever
    console.log("Infinite loop");
}

3. Using do...while loop:

do {
    // Code to run forever
    console.log("Continuous loop");
} while (true);

4. Using recursive function (be cautious, as this can cause stack overflow):

function infiniteRecursion() {
    console.log("Recursive forever");
    infiniteRecursion(); // Calls itself indefinitely
}
infiniteRecursion();

If you want a loop that can be controlled, you might want to add a break condition or use a more sophisticated looping mechanism.

Read More: Program testing and debugging in C

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com