Skip to content

Hoisting

In one line

Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope before code execution.

In simple words

Hoisting means JavaScript processes declarations before executing the code. It is as if variable and function declarations are "hoisted" (lifted) to the top of their scope.

var declarations are hoisted and initialized to undefined — you can use the variable before its declaration line (it will be undefined). Function declarations are fully hoisted — both the name and the function body, so you can call them before they appear in the code.

let and const are hoisted but NOT initialized — they exist in the "temporal dead zone" (TDZ) until their declaration line. Accessing them before the line throws a ReferenceError. Only the declaration is hoisted, not the assignment.

Example

javascript
console.log(greet()); // "Hi" — function declaration hoisted
function greet() { return "Hi"; }

console.log(x); // undefined — var hoisted
var x = 5;

Function declarations are fully hoisted. var is hoisted and initialized to undefined.

Common confusions

  • Confused with: temporal dead zone

    The difference: Hoisting moves the declaration to the top; the TDZ is the period between hoisting and initialization where let/const variables exist but cannot be accessed.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary