Chirag Sharma’s Post

🚀 WHY CONST AND LET AREN’T LIKE VAR IN JAVASCRIPT If you’ve ever wondered why const “isn’t hoisted”… Here’s the truth 👇 All variables in JavaScript are hoisted, but they’re initialized differently. 🧠 Memory Creation Phase (Before Code Runs) • Keyword Hoisting: Yes, with undefined. • Initialization: Yes. • Access Before Declaration: Allowed, resulting in ReferenceError for `let` and `const`, but undefined for `var`. ReferenceError💡 The time between scope entry and declaration is called the Temporal Dead Zone (TDZ) — the variable exists but can’t be accessed yet. console.log(a); // undefined console.log(b); // ❌ ReferenceError console.log(c); // ❌ ReferenceError var a = 10; let b = 20; const c = 30; ⚙️ What Happens Internally (Visualization) |------------------ JavaScript Execution Context ------------------| | Memory Phase (Hoisting) | var a → undefined | let b → <uninitialized> (TDZ) | const c → <uninitialized> (TDZ) |------------------------------------------------------------------| | Execution Phase | console.log(a) → undefined | console.log(b) → ReferenceError | console.log(c) → ReferenceError | a = 10; b = 20; c = 30; | console.log(a,b,c) → 10 20 30 |------------------------------------------------------------------| ✅ In short: "const and let are hoisted — but remain uninitialized until their declaration is executed." This behavior avoids unexpected bugs and makes JavaScript more predictable 💪 #JavaScript #WebDevelopment #Learning #Programming #CodeNewbie #Frontend

To view or add a comment, sign in

Explore content categories