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

This question already has answers here:
JavaScript Infinitely Looping slideshow with delays?
(11 answers)
Closed 8 years ago.
I want to create an infinite loop in JavaScript.
What are some ways to achieve this:
eg
for (var i=0; i<Infinity; i++) {}

You can also use a while loop:
while (true) {
//your code
}

By omitting all parts of the head, the loop can also become infinite:
for (;;) {}

Related

Does `for (;;)` (two semicolons) in JavaScript mean infinity? [duplicate]

This question already has answers here:
Two semicolons inside a for-loop parentheses
(4 answers)
Empty for loop - for(;;)
(4 answers)
Closed 3 months ago.
It's in JavaScript not C language, so I think it's not duplicate
I was testing some code and I found this one :-
for (;;) {
console.log("test");
}
And the iterations kept going forever
I was wondering what does this ;; mean? And what is its use case?
PS :- don't run it as it will freeze for infinite iteration.
The reason that for(;;) loops forever is because for has three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:
for(i = 0; i < 10; i++)
If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though true were there in its place. So for(;;) is the same as for(;true;), which is the same as while(true).

How to add number with variable name? [duplicate]

This question already has answers here:
How do I create dynamic variable names inside a loop?
(8 answers)
Closed 3 years ago.
Hi I want to change the variable name like stop1, stop2, stop3 etc in a loop.
I tried using for loop with stop + i but it didnt work
Please help
var varMap = {};
for(var i=1; i<10; i++) varMap['stop'+i] = 123;
I think this will help

i is not incremented within JavaScript for loop [duplicate]

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Javascript: Why do I need to declare var i = 0 in the for loop?
(4 answers)
Closed 4 years ago.
for (i = 1; i < this.people.length; i++) {
peoplePicks[i] = this.people[i].chooseAction(peopleChoices[i]);
}
I have this for loop within my JavaScript program. It runs for ever, even though the length of the array that I am passing to it is 2. when I print the value of i after the statement of the for loop, I get 0. So it seems like i is being decremented by executing the for loop's statement. How can I fix this?
Add var before your i variable in the initialising of your for loop.
Like this for (var i = 1; i < this.people.length; i++) {

For loop with two arguments in Coffeescript [duplicate]

This question already has answers here:
Decrementing for loop in coffeescript
(6 answers)
Closed 8 years ago.
I'm trying to implement this loop with Coffeescript
for( var i = arr.length; i--; )
Any ideas?
for i in [arr.length-1..0] by -1
# Something!

Javascript Break Statement Goodness [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to break from nested loops in Javascript?
Is using labels to break loops, a good practice in javascript? Mention if it has any pros and cons
Ex:
var i, j;
outer:
for(i in [0,1,2,3]) {
inner:
for(j in [0,1,2,3]) {
if(j == 1) {
break outer;
}
}
console.log("inner")
}
console.log("outer");
Yes, it is the best way to break out of multiple loops.

Categories