Explain hoisting in JavaScript. How do var, let, const, and function declarations behave differently?
Answer
Hoisting is JavaScript's default behavior of moving declarations to the top of their scope during the compilation phase. However, only declarations are hoisted, not initializations.
var: hoisted and initialized with undefined
let/const: hoisted but NOT initialized (Temporal Dead Zone)
Function declarations: fully hoisted (both name and body)
Function expressions: only the variable is hoisted (not the function body)
Creation phase, execution phase, and the TDZ. Switch between var, let, const and function forms under Tweak.
Creation phase: The engine registers every declaration in the scope before running code.
var: Initialised to undefined, so early reads return undefined.
let / const: Registered but uninitialised: reading before the declaration throws a ReferenceError (the temporal dead zone).
Function declarations: Hoisted with their body, so they can be called before their line.
Function expressions: Follow their variable's rules: var greet is undefined, so calling it throws TypeError.
Code examples
var Hoisting
console.log(x); // undefined (not ReferenceError)
var x = 5;
console.log(x); // 5
// The engine sees it as:
// var x; // declaration hoisted
// console.log(x); // undefined
// x = 5; // assignment stays
// console.log(x); // 5
var declarations are hoisted and initialized with undefined. The assignment stays in place.
let/const and Temporal Dead Zone
// This throws ReferenceError
try {
console.log(y);
let y = 10;
} catch (e) {
console.log(e.message);
}
// The TDZ exists from the start of the block
// until the declaration is encountered
{
// TDZ starts here for 'z'
// console.log(z); // would throw ReferenceError
let z = 20; // TDZ ends here
console.log(z); // 20
}
let and const are hoisted but remain in a 'Temporal Dead Zone' until the declaration line is reached.
Function Declaration vs Expression
// Function declaration - fully hoisted
console.log(greet('Alice'));
function greet(name) {
return `Hello, ${name}!`;
}
// Function expression - only variable hoisted
try {
console.log(farewell('Bob'));
} catch (e) {
console.log(e.message);
}
var farewell = function(name) {
return `Bye, ${name}!`;
};
Function declarations are fully hoisted. Function expressions with var hoist the variable as undefined, causing a TypeError when called.
Key points
Hoisting happens during the creation phase of the execution context
let and const ARE hoisted - they just can't be accessed before declaration (TDZ)
Class declarations are also in the TDZ like let/const
Common follow-up questions
What exactly happens during the creation phase?
Why was the TDZ introduced with let/const?
What is the output of: console.log(typeof undeclaredVar)?
Interview tips
Many candidates say let/const are not hoisted - this is wrong. They ARE hoisted but in TDZ
Always explain the WHY - TDZ prevents using variables before they're ready
Draw a timeline showing declaration phase vs execution phase
Concepts covered
Hoisting, TDZ, var, let, const, Function Declaration