CodeZettaInterview Hub

JavaScript · Execution Context · Junior · Concept

What happens during the creation phase vs the execution phase of an execution context?

Short Interview Answer

Creation sets up the environment (hoisting bindings, resolving `this`); execution runs the code line by line and assigns values.

Detailed Explanation

In the creation phase, the engine scans the code and allocates memory: function declarations are fully initialized, `var` bindings are created and set to `undefined`, and `let`/`const` bindings are created in the Temporal Dead Zone (uninitialized). The outer lexical environment link and `this` are also determined. In the execution phase, statements run in order: assignments happen, functions are invoked (pushing new contexts), and expressions are evaluated. This two-phase model is why you can call a function declaration before its line, but accessing a `let` before initialization throws.

Example

console.log(a); // undefined (var created as undefined)
var a = 1;
console.log(b); // ReferenceError (TDZ)
let b = 2;

Interview Tip

Mention TDZ explicitly when contrasting `var` with `let`/`const` during creation.

Common Mistake

Saying variables are 'moved to the top' as source rewriting. Hoisting is about binding creation, not physically moving code.

Did you know this answer?