Sub topic 7
Creation and Execution: Why Hoisting Happens
This chapter assumes you can declare variables with var, let, and const, write and call functions, and run code in a browser console or Node.js. It also builds on the earlier chapters about function scope, block scope, and the scope chain.
So far you have seen where a name is visible. This chapter explains when a name exists. JavaScript does not run a scope's code in a single pass from top to bottom. It first prepares the scope, and only then runs it. That two-step process is what people call hoisting.
Two Phases Every Time a Scope Is Entered
Each time the engine enters a scope (the script, a function call, or a block), it works in two phases:
- Creation phase. The engine scans the scope for declarations and creates a binding for each one. A binding is the link between a name and its storage slot. The specification calls this "declaration instantiation."
- Execution phase. The engine runs the statements in order. Assignments, function calls, and
console.loglines happen here.
The two phases run again on every call. Calling a function twice gives you two separate sets of bindings, each created from scratch.
What the Creation Phase Gives Each Declaration
Every declaration gets its binding during the creation phase. The kinds differ in the starting state of that binding:
| Declaration | Binding created in creation phase? | Initial state before its line runs |
|---|---|---|
| `var x` | Yes, in the enclosing function or script | `undefined` |
| `function f() {}` | Yes | The full function object |
| `let x` / `const x` | Yes, in the enclosing block | Uninitialized |
| `class C {}` | Yes, in the enclosing block | Uninitialized |
"Uninitialized" means the name exists, but reading it throws. The rest of the tutorial follows from this table. The next three chapters cover each row in detail.
Seeing the Phases in Action
function show() {
console.log(typeof helper); // "function"
console.log(count); // undefined
var count = 3;
function helper() {}
console.log(count); // 3
}
show();Here is what happens when show() is called:
- Creation phase:
countis bound and set toundefined.helperis bound and set to its function object. - Execution phase: The first log finds
helperalready holding a function. The second log findscountholdingundefined. The linevar count = 3now acts only as the assignmentcount = 3. The last log prints3.
Common Misconceptions
- "Only
varis hoisted." All declarations create their bindings early.let,const, andclassjust start uninitialized instead ofundefined. - "Assignments are hoisted too." Only the declaration takes part in the creation phase. The
= 3part runs during execution, at its own line. - "Hoisting happens once per file." It happens every time a scope is entered, including each function call and each pass into a block.
Exercise
Predict the four lines this code prints, and say which phase explains each one. function quiz() { console.log(a); console.log(typeof b); var a = "A"; function b() { return "B"; } console.log(a); console.log(b()); } quiz();
Show answer
It prints undefined, "function", "A", "B". In the creation phase, a is bound to undefined and b is bound to its function object. In the execution phase, the first two logs read those starting values. The line var a = "A" assigns the string. The last two logs then read "A" and call b, which returns "B".
Exercise
A function contains var total = 0; and is called twice. Does the second call start with the total left over from the first call? Why or why not?
Show answer
No. Each call runs its own creation phase and makes a new binding for total, set to undefined. The execution phase then assigns 0. The bindings from the first call are not reused.