ES6 - Compare Scopes of the var and let Keywords

Hello! I’m on the scope comparison of the var and let keywords. Now, I understand var will declare a global scope and let will be more local… But I don’t understand exactly how some of the execution is taking place within one of the example pieces of code, see below:

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

What I don’t get about this piece of code, is once i === 2 is reached,
What exactly is happening at the
printNumTwo = function () { return i; } statement? Because printNumTwo starts as an undefined variable, now it’s being defined with function () { return i; } … When I put this in python tutor, once it reaches this line it goes back up to the start of the for loop, and i = 2 becomes i = 3? But why? I don’t understand that at all… Not sure why triggering function () does this, or maybe it’s the relationship between storing function () into the undefined variable that I am not understanding?

Also, the return statement inside printNumTwo = function () { return i; }. Where is return i returning to?? As I understood it, the return statement was just to return a value to the function call… But I don’t quite understand where that value is returning to because the function wasn’t ever called? Again, this may be a relationship between printNumTwo = function() that I’m not understanding. That line of code really messed with me.

Thank you, have a nice day.

Challenge: ES6 - Compare Scopes of the var and let Keywords

Link to the challenge:

The code

printNumTwo = function() {
  return i;
};

is not triggering a function. It is defining a function. At that point, the variable printNumTwo holds a function, but that function has not been triggered (called, executed, invoked) yet. It isn’t triggered until this line:

console.log(printNumTwo());

The parens after printNumTwo cause the function stored in printNumTwo to be triggered.

So when i === 2 and printNumTwo is set to a function then the for loop moves on to the next value of i, which is 3, thus causing it to end the loop.

It returns the value of i just like any other function would return a value. In this case, when we get to the line:

console.log(printNumTwo());

Then printNumTwo will return the value of i and that value is printed to the console.

And to piggyback off of this, when you use the var keyword, i is hoisted into the global scope because i is not contained within a function, which means that it’s available after the for loop execution. Remember that var variables have function scope and let and const variables have block scope (they’re only “alive” within a block of code i.e. curly braces).

This challenge is about how just using let i = 0 instead of var i = 0 solves that problem by only having i be available inside of the for loop because it is only available to that block of code.

Thank you.
Did not realize printNumTwo() caused the triggering of the function stored in that variable.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.