Just [[currentUser.getNumFreeQuestionsLeft()]] No more free questions left!Upgrade Now
It's not "outside".
It's not "inside".
The script won't throw an error!
The console will log undefined.
Blocks like if statements and for loops do not create a new scope (this is also true of Python and recent versions of Ruby, but untrue of Java and C):
Declaration vs. assignment. A variable declaration simply tells the interpreter that a variable exists. By default it initializes the variable to undefined:
A variable assignment assigns a value to the variable:
We can both declare and assign in the same line:
So returning to the original problem:
The declaration (but not the assignment) of text gets hoisted to the top of logIt. So our code gets interpreted as though it were:
So we have a new variable text inside of logIt that is initialized to undefined, which is what it holds when we hit our log statement.
Hoisting can cause unexpected behavior, so a good way to keep things clear is to always declare your variables at the top of the scope.