Hoisting is one of the most important internal behaviors in JavaScript. It explains why certain variables and functions can appear accessible before they are declared in the code.
Many beginners find hoisting confusing because JavaScript execution does not always behave exactly as the code visually appears.
Understanding hoisting helps developers learn:
- Variable behavior
- Function declarations
- Execution context
- JavaScript memory creation
- Scope behavior
Hoisting is closely connected to how JavaScript prepares code before execution begins.
What is Hoisting?
Hoisting is JavaScript's default behavior of moving declarations to the top of their scope during the memory creation phase.
This does not mean the code physically moves. Instead, JavaScript internally registers variables and functions before execution starts.
Hoisting applies differently to:
varletconst- Function declarations
Variable Hoisting with var
Variables declared using
var
are hoisted and initialized with
undefined.
console.log(username);
var username = "Alex";
Output:
undefined
JavaScript internally treats the code like this:
var username;
console.log(username);
username = "Alex";
Hoisting with let and const
Variables declared using
let
and
const
are also hoisted,
but they are not initialized immediately.
Accessing them before declaration causes an error.
console.log(age);
let age = 25;
Output:
ReferenceError
This happens because of something called the Temporal Dead Zone.
Temporal Dead Zone (TDZ)
The Temporal Dead Zone is the period between entering a scope and the actual variable declaration.
During this period,
variables declared with
let
and
const
cannot be accessed.
This behavior helps prevent accidental usage before initialization.
Function Hoisting
Function declarations are fully hoisted in JavaScript.
This means they can be called before they appear in the code.
greet();
function greet() {
console.log("Welcome");
}
Output:
Welcome
JavaScript stores the entire function in memory during the creation phase.
Function Expressions Are Different
Function expressions behave differently because they are treated like variables.
sayHello();
var sayHello = function() {
console.log("Hello");
};
Output:
TypeError
Only the variable is hoisted, not the assigned function.
Why Hoisting Exists
During execution, JavaScript first scans the code and creates memory for variables and functions.
This preparation stage allows JavaScript to manage execution efficiently.
Hoisting is part of the execution context creation phase.
Real-World Importance
Understanding hoisting helps developers:
- Debug undefined errors
- Avoid accidental variable usage
- Understand execution order
- Write more predictable code
Many interview questions also focus heavily on hoisting behavior.
var vs let vs const Hoisting
| Keyword | Hoisted | Accessible Before Declaration |
|---|---|---|
| var | Yes | Yes (undefined) |
| let | Yes | No |
| const | Yes | No |
Common Beginner Mistakes
- Assuming variables are physically moved in the code
- Using variables before declaration
- Confusing function declarations with function expressions
- Ignoring Temporal Dead Zone behavior
Best Practices
- Declare variables before using them
- Prefer let and const over var
- Avoid relying on hoisting behavior intentionally
- Keep function declarations organized clearly
Summary
Hoisting is JavaScript's internal behavior of preparing variables and functions before execution begins.
Different declaration types behave differently during hoisting,
especially
var,
let,
and
const.
Understanding hoisting helps developers write safer, more predictable, and easier-to-debug JavaScript applications.