Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I create an infinite loop in JavaScript [duplicate]

I want to create an infinite loop in JavaScript.

What are some ways to achieve this:

eg

for (var i=0; i<Infinity; i++) {} 
like image 269
Elise Chant Avatar asked Jul 27 '14 02:07

Elise Chant


People also ask

How do you make an infinite loop in JavaScript?

In for() loop: To avoid ending up in an infinite loop while using a for statement, ensure that the statements in the for() block never change the value of the loop counter variable. If they do, then your loop may either terminate prematurely or it may end up in an infinite loop.

How do you make a for loop infinite?

To make an infinite loop, just use true as your condition. true is always true, so the loop will repeat forever. Warning: Please make sure you have a check that exits your loop, otherwise it will never end.

What is a repeat forever loop?

Repeat Forever Behavior: This form of repeat loop repeats indefinitely until terminated.

How many times will an infinite loop repeat?

Details. An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.


1 Answers

You can also use a while loop:

while (true) {     //your code } 
like image 174
Pran Avatar answered Sep 21 '22 10:09

Pran