JavaScript Hoisting Explained
JavaScript hoisting is the engine's behavior of moving declarations to the top of their scope before execution. This post explains how function declarations, var variables, and let/const behave differently during hoisting, with clear code examples to illustrate each case.
If you've ever run a JavaScript function before it was defined in your code, and it somehow worked, you've experienced hoisting in action. It feels like magic at first, but there's a straightforward explanation behind it.
What Is Hoisting?
Hoisting is JavaScript's default behavior of moving declarations to the top of their scope before code executes. The JavaScript engine processes your code in two phases: a compilation phase and an execution phase. During compilation, it scans for variable and function declarations and sets them up in memory before any code actually runs.
The important word here is declarations. Hoisting moves declarations, not initializations. That distinction is the source of most hoisting-related bugs.
Function Hoisting
Function declarations are fully hoisted. That means you can call a function before you define it in your source file, and it will work without any errors.
greet("Alex");
function greet(name) {
console.log("Hello, " + name);
}
// Output: Hello, AlexThe JavaScript engine sees the function greet declaration during compilation and makes the entire function available in memory. By the time greet("Alex") runs, the function is already there.
This does not apply to function expressions. If you assign a function to a variable, only the variable declaration is hoisted, not the function itself.
greet("Alex"); // TypeError: greet is not a function
var greet = function(name) {
console.log("Hello, " + name);
};During compilation, JavaScript hoists var greet and sets its value to undefined. When the code tries to call greet("Alex"), it finds undefined instead of a function, which throws a TypeError.
Variable Hoisting with var
Variables declared with var are hoisted to the top of their function scope, but they are initialized as undefined until the assignment line is reached.
console.log(color); // undefined
var color = "blue";
console.log(color); // blueWhat JavaScript actually sees during execution looks like this:
var color; // hoisted to top, initialized as undefined
console.log(color); // undefined
color = "blue"; // assignment stays in place
console.log(color); // blueNo error is thrown because the declaration exists, but the value hasn't been assigned yet.
let and const Behave Differently
Variables declared with let and const are also hoisted, but they are not initialized. They sit in what is called the Temporal Dead Zone (TDZ) from the start of the block until the declaration is reached. Accessing them before that point throws a ReferenceError.
console.log(score); // ReferenceError: Cannot access 'score' before initialization
let score = 100;This stricter behavior is actually helpful. It catches bugs that var would silently swallow by returning undefined.
A Quick Reference
- Function declarations: fully hoisted, callable before definition
- var variables: hoisted and initialized as
undefined - let and const: hoisted but not initialized, TDZ applies
- Function expressions: follow the rules of the variable type used (
var,let, orconst)
Practical Advice
Understanding hoisting is important for debugging, but you should not write code that relies on it. Declare your variables at the top of their scope and define your functions before you call them. Use let and const instead of var by default. This makes your code easier to read and prevents subtle bugs caused by accessing variables before they have meaningful values.
What's Next
Now that you understand how JavaScript handles declarations before execution, the next logical step is exploring scope in depth. Knowing the difference between global scope, function scope, and block scope will help you predict exactly where your variables live and for how long. That's coming up in the next post in this series.