Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var funcs = [];
    
for (let i = 0; i < 10; i++) {
    funcs.push(function() { console.log(i); });
}
    
funcs.forEach(function(func) {
    func();   //How does this function retreive the variable "i"?
});


How does the func(); function retreive the variable "i"?

What I have tried:

When func() is run, does the code return to line 4, where the function was created?
Or is func() replaced with "console.log(a number)"?
Or is func() literally replaced with "console.log(i)"?
Posted
Updated 6-May-18 7:20am
v3
Comments
Thomas Daniels 7-May-18 10:14am    
Hello! You wrote another comment on my answer yesterday asking for clarification and I did not have the time to answer it back then, but now you seem to have deleted the comment.

If you still need an answer to your question in the comment, it would be this:

That is actually unrelated to the scope. An integer is a value type so when you write "var b = t;", the value of "t" is copied into "b" and from then on, the variables are independent. But again, that's unrelated of scope.

1 solution

Quote:
How does the func(); function retreive the variable "i"?

With the "return" statement, which returns a value to the caller:
JavaScript
var funcs = [];
    
for (let i = 0; i < 10; i++) {
    funcs.push(function() { return i; });
}
    
funcs.forEach(function(func) {
    var i = func(); // note: you do not have to call it "i", you can give this variable any valid name
    console.log(i);
});

Quote:
When func() is run, does the code return to line 4, where the function was created?
Or is func() literally replaced with "console.log(a number)"?

You don't actually "return" to line 4: when you call funcs.push(...), you create a new function that returns a value, and that value is what i is. So, once you are out of your for loops, your array contains 10 function definitions: a function that returns 0, a function that returns 1 and so on. The .forEach iterates over all these functions one by one.

In your comment, you asked why it would always print "10" if you used var instead of let. The reason is scope[^] : let has block scope, var has function scope (or when defined outside a function, like here, global scope). So when you use var, then i will always be "the same i" because it has the same scope, which does not happen with let, because it has a different scope.
 
Share this answer
 
v3
Comments
Member 13814166 6-May-18 13:24pm    
Ok, I understand, but why, if instead of let i=0 the for loop created var i=0, does the function print "10" ten times? I would expect the items of the funcs array to have the same values as when the for loop used let i=0?
Thomas Daniels 6-May-18 13:28pm    
I edited my answer; see the last paragraph.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900