The array funcs is filled with 10 closures in a for loop. The closures capture the loop variable 'i', which is declared with keyword var. Each closure simply returns the captured variable:
var funcs = [], sum=0;
for(var i=0; i < 10; ++i) {
funcs.push(function() {
return i;
});
}
The closures are called in a loop to calculate the sum of their returned values:
for(var j=0; j < funcs.length; ++j)
sum += funcs[j]();
What will be the value of sum?
console.log(sum);