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