Hoisting (Critical)
Master this topic with zero to advance depth.
Expert Answer & Key Takeaways
Mastering Hoisting (Critical) is essential for high-fidelity technical architecture and senior engineering roles in 2026.
JavaScript Hoisting
Hoisting is JavaScript's default behavior of moving declarations to the top of the current scope (to the top of the current script or the current function).
1. The Concept of Hoisting
In JavaScript, a variable can be used before it has been declared.
x = 5; // Assign 5 to x
console.log(x);
var x; // Declare xThe JavaScript engine "hoists" the declaration (
var x) to the top, so the code effectively becomes:var x; // Declaration moved to top
x = 5;
console.log(x);2. The let and const Keywords
Variables defined with
let and const are also hoisted to the top of the block, but not initialized.
The block of code is aware of the variable, but it cannot be used until it has been declared. Using a let variable before it is declared will result in a ReferenceError.
The variable is in a "Temporal Dead Zone" from the start of the block until it is declared.carName = "Volvo"; // ReferenceError: Cannot access 'carName' before initialization
let carName;3. Function Hoisting
Function declarations are hoisted completely, meaning you can call a function before it is defined in the code.
myFunction(); // This works!
function myFunction() {
console.log("Hello World");
}[!IMPORTANT] Function expressions (where you assign a function to a variable) are NOT hoisted like declarations. They follow the hoisting rules of the variable keyword (var,let, orconst) used to define them.
4. Only Declarations are Hoisted
JavaScript only hoists declarations, not initializations.
var x = 5;
var y = 7;
console.log(x + " " + y); // "5 7"
// vs
var a = 5;
console.log(a + " " + b); // "5 undefined"
var b = 7;In the second example, only the declaration
var b is hoisted, not the value 7. Therefore, b is undefined when the console.log runs.Top Interview Questions
?Interview Question
Q:What is Hoisting in JavaScript?
A:
Hoisting is the behavior where the JavaScript engine moves variable and function declarations to the top of their containing scope during the compilation phase, before the code is executed.
?Interview Question
Q:What is the Temporal Dead Zone (TDZ)?
A:
The TDZ is the period between entering a scope and the actual line where a 'let' or 'const' variable is declared. During this time, the variable cannot be accessed, and trying to do so throws a ReferenceError.
?Interview Question
Q:Are function expressions hoisted?
A:
No. Only function declarations are fully hoisted. Function expressions follow the hoisting rules of the variable they are assigned to (e.g., if using 'const', they are in the TDZ).
Course4All Engineering Team
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.