Hoisting in JavaScript
This behavior can surprise beginners to JavaScript, and also, I admit, people like me who learned JavaScript by doing.
So what is hoisting about? Concisely put, it means that every variable declared in a scope is actually declared when entering the scope, no matter where you choose to put the declaration.
In the following examples I’m going to create a function to define a scope, but this can be applied to every scope, the global scope, a scope you define with {
and }
, etc.