With all this talk about ES6 and block scoping with
This won’t be a thorough exposition. You can find a great deal of information on GitHub. What I want to do is give the basics.
There are three parts to the compiler:
- The Engine
The interpreter takes this statement and breaks it up into what are called tokens. To the interpreter, this statement and its tokens are
The engine part of the interpreter will compile these tokens into an array and subsequent tree of nested elements to execute. The code is compiled right before it is executed.
What is Meant by Hoisting?I am assuming here that you know what scope is, as I have talked about block scoping before with the same assumption. If you don’t, I’ll cover it in more detail another time. For now, I am just going to focus on hoisting.
Dictionary.com definition of hoist:
You can think of hoisting a variable as lifting it to the top of whatever scope it is in.verb (used with object)1.
to raise or lift, especially by some mechanical appliance:
to hoist a flag; to hoist the mainsail.
console.log(foo); var foo = 3;
Take this statement:
var foo = 3;
var foo; and
foo = 3;
The first part of the statement is compiled, the second part is executed. It is then compiled as:
var foo; console.log(foo); foo = 3;
As you can see, the declaration in this scope is the only thing that was “raised” to the top of the scope. The initialization remains at the bottom.