JavaScript Hoisting Explained: Variables and Memory Preparation

💡 Hoisting in JavaScript looks simple… until you see what’s happening under the hood At first glance, hoisting feels easy — “variables are moved to the top” — but the reality is much deeper. In reality, JavaScript creates all variables in the current scope before it even starts executing the code. Here’s where things get interesting 👇 🔹 Variables declared using var → Created and initialized with undefined 🔹 Variables declared using let and const → Created but marked as  → Accessing them before initialization throws a ReferenceError As described in the ECMAScript spec: 👉 For let and const: Variables are created when their containing Environment Record is instantiated, but cannot be accessed until their LexicalBinding is evaluated. 👉 For var: Variables are created when their Environment Record is instantiated and are initialized to undefined immediately. 💭 The takeaway? Hoisting isn’t just about moving declarations — it’s about how JavaScript prepares memory before execution even begins. And that’s why something that looks so simple on the surface actually has a lot going on behind the scenes. 🙏 Credit: GreatFrontEnd for the clear explanation and breakdown. #JavaScript #Hoisting #WebDevelopment #Frontend #Programming #LearnInPublic

  • diagram

To view or add a comment, sign in

Explore content categories