Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 2 of 10 · JavaScript Basics & Fundamentals
Interview question

What is hoisting in JavaScript and how does it work? JavaScript में hoisting क्या है और यह कैसे काम करता है?

Answer

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

// Variable hoisting
console.log(x); // undefined (not ReferenceError)
var x = 5;

// Function hoisting
greet(); // 'Hello' - works because function is hoisted
function greet() { console.log('Hello'); }

// let/const hoisting (Temporal Dead Zone)
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 10;
TypeHoisted?Initialized to?Accessible before declaration?
var declarationYesundefinedYes (gives undefined)
let/const declarationYes (in TDZ)Not initializedNo (ReferenceError)
Function declarationYesFunction bodyYes
Function expressionNoN/ANo

Interview tip: Explain that hoisting happens at compile-time, not runtime. All declarations (var, let, const, function) are hoisted, but var is initialized to undefined while let/const enter a Temporal Dead Zone.

Hoisting JavaScript का एक behavior है जहाँ declarations को उनके scope के top पर move किया जाता है code execution से पहले।

console.log(x); // undefined (ReferenceError नहीं)
var x = 5;

greet(); // 'Hello' - काम करता है क्योंकि hoisted है
function greet() { console.log('Hello'); }

console.log(y); // ReferenceError
let y = 10;
TypeHoisted?Initialized to?Declaration से पहले accessible?
varहाँundefinedहाँ (undefined देता है)
let/constहाँ (TDZ में)नहींनहीं (ReferenceError)
Function declarationहाँFunction bodyहाँ
Function expressionनहींN/Aनहीं

इंटरव्यू टिप: समझाएं hoisting compile-time पर होता है। सभी declarations hoisted होते हैं, पर var को undefined में initialize किया जाता है जबकि let/const को TDZ में रखा जाता है।

Was this answer clear?