JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Hoisting

javascript
junior
hoisting

How does JavaScript hoisting work?

hoisting
scope
var
let
const
temporal-dead-zone
fundamentals
Quick Answer

Hoisting moves variable and function declarations to the top of their scope during compilation. var declarations are initialized as undefined, while let/const enter a Temporal Dead Zone until their declaration is reached.

Detailed Explanation

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

Function Declarations:

  • Fully hoisted — both the name and the body are available before the declaration
  • Can be called before they appear in the code

var:

  • Declaration is hoisted and initialized as undefined
  • Assignment stays in place
  • Accessing before assignment returns undefined

let and const:

  • Declaration is hoisted but not initialized
  • Accessing before declaration throws ReferenceError
  • The zone between scope start and declaration is called the Temporal Dead Zone (TDZ)

Function Expressions and Arrow Functions:

  • Follow the hoisting rules of their variable keyword (var/let/const)
  • Only the variable name is hoisted, not the function body

Code Examples

Hoisting behavior by declaration typeJavaScript
// Function declarations are fully hoisted
sayHello(); // 'Hello!' — works fine
function sayHello() {
  console.log('Hello!');
}

// var is hoisted as undefined
console.log(x); // undefined (not an error)
var x = 5;

// let/const enter the Temporal Dead Zone
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 10;

// Function expressions follow variable rules
console.log(greet); // undefined (var hoisted)
greet(); // TypeError: greet is not a function
var greet = function() {
  console.log('Hi!');
};

Real-World Applications

Use Cases

Legacy Code Maintenance

Understanding var hoisting bugs when maintaining older JavaScript codebases that predate ES6

Code Review

Spotting subtle hoisting-related bugs during code reviews, especially with var declarations inside loops or conditionals

Debugging

Understanding why a variable is undefined instead of throwing a ReferenceError when accessed before its assignment

Mini Projects

Hoisting Quiz

beginner

Build a game showing code snippets and asking users to predict the output based on hoisting rules

Code Output Predictor

intermediate

Build a tool that shows step-by-step how the JS engine hoists declarations before executing code

Industry Examples

ESLint

The no-use-before-define rule warns when variables, functions, or classes are referenced before their declaration

V8

V8's parser identifies all variable and function declarations during the parsing phase before execution, implementing hoisting at the engine level

Resources

MDN - Hoisting

docs

JavaScript.info - Variable scope, closure

article

Related Questions

What is the difference between var, let, and const in JavaScript?

junior
variables

What is a closure in JavaScript and why are they useful?

mid
closures
Previous
What is the difference between arrow functions and regular functions?
Next
What is event delegation in JavaScript and why is it useful?
PrevNext