Trying to clarify "stored function"

This exercise
features a “stored function” printNumTwo = function() :

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

I don’t quite understand what is happening when the loop passes over that function, because I thought nothing would execute after the return i statement.
But I put a console.log("this is " + i) just after the if statement closing bracket (inside the for loop). It logs

this is 0
this is 1
AND it logs
this is 2

So am I right in understanding that the “stored function” is just waiting until the for loop is completely finished to execute? (like recursion)

Actually the “stored function” is waiting until it’s called to execute. What is happening inside the loop is the function declaration.

Basically, you are storing that function in the variable printNumTwo, but the function itself isn’t executed. It will only execute when your code reaches printNumTwo().

It’s just like declaring a function anywhere else in the code.

1 Like

Oh duh!
Thank you kindly, I have been working so long, I ignored the last console.log, which is actually calling it!