Execution Context & Hoisting — JavaScript interview guide
Execution Context & Hoisting
Before JavaScript runs your code, it creates an execution context and sets aside memory for variables and functions. That memory step is why hoisting happens.
See it run
Press Play, or step with Next. Watch the highlighted line, the Call Stack and memory change together.
Creation phase vs execution phase
1console.log(name);2console.log(greet());34var name = "Sumit";5let age = 24;67function greet() {8return "Hello " + name;9}1011console.log(age);
no variables
nothing printed yet
What happens:Before running a single line, JavaScript creates the Global Execution Context and pushes it onto the Call Stack.
Tip: click the box and use ← → keys to step, space to play.
Explained simply
What is an execution context?
An execution context is the "box" in which a piece of code runs. It holds the variables, the functions and the value of this for that code.
There is one Global Execution Context for the whole file, and a new Function Execution Context every time a function is called. They are stacked on the Call Stack.
Two phases: creation, then execution
Creation phase: JS scans the code and reserves memory. var gets undefined, let and const stay uninitialized, function declarations are stored completely.
Execution phase: JS runs the code line by line and assigns the real values.
Hoisting and the Temporal Dead Zone
Hoisting is the result of the creation phase: declarations are known before the code runs. Only the declaration is hoisted, never the assigned value.
let and const are hoisted too, but reading them before their line throws a ReferenceError. The time between the start of the scope and the declaration line is the Temporal Dead Zone (TDZ).
console.log(a); // undefined (var is hoisted with undefined)
console.log(b); // ReferenceError: Cannot access 'b' before initialization
var a = 1;
let b = 2;
sayHi(); // works — function declarations are fully hoisted
function sayHi() { console.log("hi"); }
hello(); // TypeError: hello is not a function
var hello = function () {};Remember this
- Every function call creates a new execution context on the Call Stack.
- var → undefined, let/const → TDZ, function declarations → whole function.
- Function expressions and arrow functions follow the rules of the variable they are stored in.
- Hoisting moves declarations, not assignments.
Interview questions
What is the output of console.log(x); var x = 5;?
undefined. The declaration of x is hoisted and set to undefined in the creation phase; the value 5 is assigned only when line 2 runs.
Are let and const hoisted?
Yes, they are hoisted, but not initialized. Accessing them before the declaration throws a ReferenceError because they are in the Temporal Dead Zone.
Why can you call a function declaration before it is written, but not a function expression?
A function declaration is stored completely in the creation phase. A function expression is just a value assigned to a variable, so with var the variable is undefined and calling it throws a TypeError.
Practice
- Predict the output when a function and a var share the same name.
- Explain the difference between ReferenceError and TypeError using hoisting examples.