Online Compiler logoOnline Compiler

JavaScript Tutorial

JavaScript Hoisting — Complete Guide to Variables and Functions

Hoisting moves declarations to the top of their scope during compilation.

Understanding how var, let, const, and functions behave prevents confusing runtime bugs.

Why We Need It

Hoisting explains many interview questions and surprising behaviors in JavaScript.

Knowing TDZ and function hoisting leads to safer, more predictable code.

Syntax

console.log(name); // undefined
var name = "Asha";
// let/const throw before declaration

Basic Example

1. Basic Hoisting Concept

console.log(message); // undefined
var message = "Hello World";

Declarations are hoisted, but assignments are not.

Real World Example

2. var vs let (TDZ)

console.log(a); // undefined
var a = 1;

// console.log(b); // ReferenceError
let b = 2;

var is initialized to undefined; let is in TDZ until declared.

Multiple Use Cases

What is Hoisting?

Hoisting moves declarations to the top of their scope during compilation.

Only declarations are hoisted, not assignments, which explains many surprises.

Modern JavaScript uses let/const and TDZ to make hoisting safer and more predictable.

var vs let vs const

var is hoisted and initialized to undefined, so it can be read before assignment.

let and const are hoisted but inaccessible until declared (temporal dead zone).

const must be initialized immediately and cannot be reassigned.

Function Hoisting

Function declarations are fully hoisted, so you can call them before definition.

Function expressions follow variable hoisting rules and are not callable before assignment.

Common Pitfalls

Using var in loops causes closure bugs; let fixes this by creating per-iteration bindings.

Accessing let/const before declaration throws ReferenceError because of TDZ.

Multiple function declarations with the same name overwrite earlier ones.

More Examples

3. Function Declaration Hoisting

greet("World");

function greet(name) {
  console.log("Hello, " + name + "!");
}

Function declarations are fully hoisted.

4. Loop Pitfall

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}

for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 100);
}

var shares one binding; let creates per-iteration bindings.

5. Function Expression Hoisting

// sayHi(); // TypeError
var sayHi = function() {
  console.log("Hi!");
};

Function expressions are not hoisted with their implementation.

Comparison

Without

// var hoisted
console.log(a);
var a = 1;

With

// let in TDZ
// console.log(b);
let b = 1;

Common Mistakes and Fixes

Using var in modern code

Prefer let/const to avoid hoisting confusion.

Accessing let/const early

Declare variables before use to avoid TDZ errors.

Assuming function expressions hoist

Only function declarations are fully hoisted.

Interview Questions

What is hoisting?

Hoisting moves declarations to the top of scope during compilation.

How do let and const differ from var?

let/const are in TDZ until declared; var is initialized to undefined.

Are function expressions hoisted?

No. Only function declarations are fully hoisted.

Practice Problem

Practice: Predict the output and then run the code.

console.log(x);
var x = 10;

// console.log(y);
let y = 20;

One Possible Solution

// Output:
// undefined
// ReferenceError for y if uncommented

Frequently Asked Questions

What is hoisting in JavaScript?

Hoisting moves declarations to the top of their scope during compilation.

What is the temporal dead zone?

It is the time before let/const declaration when the variable exists but cannot be accessed.

Are function expressions hoisted?

No. Only function declarations are hoisted with their body.

Why avoid var?

var is function-scoped and hoisted to undefined, which can cause bugs.

Try It Yourself

Run the basic hoisting example and observe the output.