This question already has answers here:
Explanation of `let` and block scoping with for loops
(5 answers)
Difference between let and var inside setTimeout? [duplicate]
(3 answers)
Why let and var bindings behave differently using setTimeout function? [duplicate]
(2 answers)
Closed 5 years ago.
There is something I didn't really understand clearly, If somebody could help me:
let arr = [1,2,3,4,5,6,7];
let b = 0
for (let a of arr) {
setTimeout(() => {
console.log(b, a)
}, 2000)
b = b + 1;
}
/* Output
7 1
7 2
7 3
7 4
7 5
7 6
7 7
*/
Let's say b is equal to 7 because 2 seconds later, the variable b is equal to 7, then why a has a different behaviour than b ?
The difference is how scoped variables work with the let keyword. In this case, you are defining a new a for each iteration of the loop. But it is the same b variable for every iteration.
The timeouts don't actually start firing until after the loop ends. By this time, each timeout function scope was given a different a, but are grabbing the same final value of b.
Related
This question already has answers here:
Swapping two variable value without using third variable
(31 answers)
How does XOR variable swapping work?
(11 answers)
Closed 10 months ago.
I found out a neat trick to swap the values of two variables without having to create an auxiliar variable. I'll use javascript to illustrate it, although I'm guessing it's language agnostic.
let a = 1;
let b = 2;
a^=b; b^=a; a^=b;
console.log(a); // Prints 2
console.log(b); // Prints 1
Can someone explain how does the swap happen? What does ^= do?
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!
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
let keyword in the for loop
(3 answers)
Closed 4 years ago.
Why output is 10 when use var and with let it works the loop.
Edit the output is 11 but why let and var are different
var funcions = [];
for (var x = 0; x <= 10; x++) {
funcions.push(function() {
console.log(x);
});
}
funcions.forEach(
function(func) {
func();
}
);
I know that let, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.
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
This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 5 years ago.
Here is the code:
var num = 3
myFunction(num)
function myFunction(aNumber)
{
aNumber = 20
}
console.log(num)
it still says that 'num' is 3 and I was wondering why?
You dont have to pass global variables into functions to reassign them.
function myFunction() {
num = 20
}
console.log(num);