Confused about a closures in Javascript and global variables

Viewed 14

I understand that var cause a single binding for an i in a loop.

Can somebody explain why the answer is 5? Does i++ increments one more time on a function call?

Try to debug it, but still confused about workings of a loop.

var result = [];

for(var i = 0; i < 5; i++){
  result[i] = function(){
    return i;
  }
}
console.log(result[0]())
0 Answers
Related