#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read
•Question 4 of 50easy

What is Hoisting in JavaScript?

Understanding variable and function hoisting.

What You'll Learn

  • What hoisting is
  • How var, let, const behave
  • Function hoisting

What is Hoisting?

Hoisting is JavaScript's behavior of moving declarations to the top of their scope during compilation.

Variable Hoisting

code.jsJavaScript
console.log(x); // undefined (hoisted but not initialized)
var x = 5;

console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 5;

Function Hoisting

code.jsJavaScript
// Function declarations are fully hoisted
sayHello(); // Works!
function sayHello() {
  console.log('Hello');
}

// Function expressions are NOT hoisted
greet(); // TypeError: greet is not a function
var greet = function() {
  console.log('Hi');
};

Temporal Dead Zone (TDZ)

let and const are hoisted but not initialized, creating a TDZ from the start of the block until the declaration.