Why does the first loop result to 0 instead of 1? [duplicate] - javascript

This question already has answers here:
trivial for-loop needs an explanation
(4 answers)
Closed 5 years ago.
I don't understand why the first loop prints out 0 instead of 1. Doesn't the i++ apply to the first loop?
for (i = 0; i < 3; i++) {
console.log('i');
}

No i++ increments only after executing logic in the loop

Related

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++) {

console.log() on setTimeout() JS [duplicate]

This question already has answers here:
setTimeout calls function immediately instead of after delay
(2 answers)
JavaScript setTimeout() won't wait to Execute? [duplicate]
(3 answers)
setTimeout in for-loop does not print consecutive values [duplicate]
(10 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 years ago.
can someone help me understanding why this code:
for (var i =0; i < 2; i++) {
setTimeout(console.log(i), 0);
}
console.log("aaa");
Will write:
0
1
aaa
But that code:
for (var i =0; i < 2; i++) {
setTimeout(function(){console.log(i)}, 0);
}
console.log("aaa");
Will wirte that:
aaa
2
2
Note that I understand how the second vers. work, I don't get why the first one make it differnt.
Thanks!

setTimeout inside for loop with Interval 0 [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Is setTimeout with no delay the same as executing the function instantly?
(4 answers)
Closed 5 years ago.
when I print "i" inside setTimeout with time diff 0. why does it print "n" the same number always?
for(i = 0; i < 5; i++) {
setTimeout(function() {console.log(i)}, 0);
}
Output:
5
5
5
5
5

How to bind your variables to setTimeout in javascript? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 5 years ago.
for(i = 0; i < 10; i++){
setTimeOut(function(){
console.log(i);
},2000);
}
When I execute this, its printing 10, 10 times instead of 1,2,3....10. How do I fix this
You need to modify your code like this
function print(i){
console.log(i);
}
for(i=0;i<10;i++){
setTimeout(print.bind(null,i),2000);
}

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!

Categories