JavaScript Hoisting Explained: Memory Creation Phase

PART 1 — What Hoisting Really Is (Mental Model) 📌 Frontend Interview Series — JavaScript Hoisting (Part 1) Hoisting is not JavaScript “moving code to the top.” That explanation causes more confusion than clarity. Most candidates memorize: “Declarations are hoisted.” But struggle when interviewers ask: “What actually happens under the hood?” Here’s the interview-ready mental model. JavaScript executes code in two phases: 1️⃣ Memory Creation Phase   Before a single line runs: • Memory is allocated for variables and functions   • var → initialized as undefined   • function declarations → stored with full function body   • let / const → allocated but uninitialized  2️⃣ Code Execution Phase   • Code runs line by line   • Values are assigned   • Functions are executed  Hoisting is a side-effect of the memory creation phase. No code is physically moved. Key insight: Hoisting is about *when memory is assigned*, not *where code is written*. Next: If let and const are hoisted too, why do they still throw errors? #JavaScript #Hoisting #FrontendInterview #LearningInPublic #Frontend

let and const are hoisted but still throw reference error because They are in TDZ (Temporal Dead zone) Basically the variables declared through let and const enters TDZ when no value has been assigned to them means these variables have not even initialized What exactly is TDZ -> Basically it is a time between entering a scope and declaration of variable in which the variable becomes in accessible

Another AI copy paste

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories