JavaScript Hoisting Explained: Var, Let, and Const

🎯 Today in my mock interview, I was asked: What is Hoisting in JavaScript? Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their scope during the compilation phase. But behavior depends on how the variable is declared: 🔹 var → Hoisted and initialized with undefined 🔹 let & const → Hoisted but not initialized (Temporal Dead Zone) Example: console.log(a); // undefined var a = 10; console.log(b); // ReferenceError let b = 20; console.log(c); // ReferenceError const c = 30; 📌 Important: var can be accessed before declaration (returns undefined) let and const cannot be accessed before declaration They remain in the Temporal Dead Zone (TDZ) Mock interviews are helping me strengthen my fundamentals daily 🚀 #javascript #frontenddeveloper #interviewprep #reactjs #webdevelopment

To view or add a comment, sign in

Explore content categories