Programming Languages JavaScript Subjective
Sep 25, 2025

What is hoisting in JavaScript?

Detailed Explanation

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

Variable Hoisting:


console.log(x); // undefined (not error)
var x = 5;

// Equivalent to:
var x;
console.log(x); // undefined
x = 5;

Function Hoisting:


sayHello(); // "Hello!" - works

function sayHello() {
console.log("Hello!");
}

let/const Hoisting:


console.log(y); // ReferenceError
let y = 10;

let and const are hoisted but not initialized, creating a "temporal dead zone".

Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback