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

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!

Related

Why does the JS increment shorthand not working [duplicate]

This question already has answers here:
What's the difference between ++i and i++ in JavaScript [duplicate]
(6 answers)
javascript i++ vs ++i [duplicate]
(8 answers)
Closed 3 years ago.
I am trying to write a very simple script but do not understand why one syntax isn't working over the other.
The function is to simply increment any number by one.
This one does not work
function plusOne(x) {
return x++;
}
but this one does.
function plusOne(x) {
return x + 1;
}
What am I not understanding??
Increment (++)
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing
You should use
function plusOne(x) {
return ++x;
}

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

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

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

why console value is 3 , I didn't Understand Closures [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
I understand closures but I don't understand how this example works:
for (var i=1; i<=5; i++) {
setTimeout( function timer(){
console.log( i );
}, i*1000 );
}
In this example it logs 6 six times with a value of six. I understand this is due to closures. However, I don't understand how. Any help would be appreciated.

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);
}

Categories