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
?