Help! - ES6: Compare Scopes of the var and let Keywords

Tell us what’s happening:
I can’t understand why the output is 3 in this challenge. The loop already encountered a return statement the loop should stop and the increment for variable i should not happen? Am I right?? or Am I missing something?

Your code so far


var printNumTwo;
for (var i = 0; i < 3; i++) {
  if (i === 2) {
    printNumTwo = function() {
      return i;
    };
  }
}
console.log(printNumTwo());
// returns 3

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36.

Challenge: Compare Scopes of the var and let Keywords

Link to the challenge:

the loop declaration is inside the function, but the function is actually executed only once it’s called in the last line outside of the loop

1 Like

Hello @ilenia! Super thanks! hehe

I was confused. The function was initialize in the variable printNumTwo! That’s why it did not stop the loop. And the value of i is 3.