Answer the question
In order to leave comments, you need to log in
Why does the loop output ten times undefined?
Hello!
Faced this situation:
for (var i = 0; i < 10; i++) {
(function() {
var i = i;
console.log(i)
})();
}
Answer the question
In order to leave comments, you need to log in
var i //Создать переменную i (undefined) в данной области видимости
= i; //Затем присвоить ей значение переменной переменной i (которую только что создали - undefined)
variable i is taken from outside and assigned to variable iIt's not like that, the name "i" is captured not at the moment of var, but from the beginning of the block to the end, the same result will be if var i is moved to the end:
for (var i = 0; i < 10; i++) {
(function() {
console.log(i);
var i;
})();
}
foo(); // вызов
function foo() {console.log('FOO') } // объявление
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question