[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$f8yo9vsu7zzfb":3},{"_id":4,"slug":5,"title":6,"subtitle":7,"kind":8,"cards":9,"tags":59,"categories":61,"source":63,"lang":66,"author":67,"audioState":70,"stats":71,"publishedAt":74,"renderer":75},"6abb6f5fca21c797c7e9cbea","under-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e","Under the Hood of JavaScript: Demystifying Hoisting, Objects, 'this', and the 'new' Keyword","You know that moment when JavaScript does something so weird you start suspecting your laptop is haunted?","news",[10,14,19,24,29,34,39,44,49,54],{"headline":11,"body":12,"imageUrl":13,"sourceImageUrl":13},"Under the Hood of JavaScript: Demystifying Hoisting, Objects, 'this', and the…","You know that moment when JavaScript does something so weird you start suspecting your laptop is haunted? Or when this is your object in one line and undefined in the next, and you didn't change anything?","https:\u002F\u002Fmedia2.dev.to\u002Fdynamic\u002Fimage\u002Fwidth=1200,height=627,fit=cover,gravity=auto,format=auto\u002Fhttps%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6f146hw0e5q6xyw3ty1i.png",{"headline":15,"body":16,"imageUrl":17,"images":18},"JavaScript isn't random. It follows a small set","JavaScript isn't random. It follows a small set of rules, and nobody explains them well on day one. Once these four ideas click, a huge chunk of \"JS magic\" turns into \"oh, that makes sense\": Grab a coffee. Let's pop the hood. ☕ 1. Hoisting: JavaScript Reads Your Code Twice Here's the mental model that fixes most confusion:","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F1.webp",{"local":17},{"headline":20,"body":21,"imageUrl":22,"images":23},"Before JavaScript runs your code, it does a","Before JavaScript runs your code, it does a quick prep pass called the creation phase. During it, JS scans the scope and registers your declarations in memory. Then the execution phase runs your code line by line.","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F2.webp",{"local":22},{"headline":25,"body":26,"imageUrl":27,"images":28},"Hoisting is just a side effect of that","Hoisting is just a side effect of that prep pass. Your code isn't physically moved anywhere. Declarations are registered before execution begins. What each declaration gets during the creation phase depends on how you declared it. Functions, var, and let\u002Fconst side by side Function declarations: fully hoisted","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F3.webp",{"local":27},{"headline":30,"body":31,"imageUrl":32,"images":33},"The entire function, body included, is stored during","The entire function, body included, is stored during the creation phase. That's why it's callable from anywhere in its scope. var: hoisted, but only the name","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F4.webp",{"local":32},{"headline":35,"body":36,"imageUrl":37,"images":38},"JS sees var coffee, creates the variable, and","JS sees var coffee, creates the variable, and sets it to undefined. The = \"espresso\" part only happens during execution. Behind the scenes it's like this: let and const: hoisted, but locked (the Temporal Dead Zone) Plain-English version of the Temporal Dead Zone (TDZ):","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F5.webp",{"local":37},{"headline":40,"body":41,"imageUrl":42,"images":43},"Imagine a restaurant reserves a table with your","Imagine a restaurant reserves a table with your name on it. The table exists, but you can't sit down until your party arrives. Touch it early and the staff yells at you.","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F6.webp",{"local":42},{"headline":45,"body":46,"imageUrl":47,"images":48},"For let and const, the variable is registered","For let and const, the variable is registered in the creation phase, but it stays off-limits until execution reaches its declaration line. The stretch between the start of the scope and that line is the Temporal Dead Zone.","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F7.webp",{"local":47},{"headline":50,"body":51,"imageUrl":52,"images":53},"The error message says \"cannot access before initialization\"","The error message says \"cannot access before initialization\", not \"tea is not defined\". That's proof tea was hoisted. JS knows about it but refuses to let you touch it yet. Gotchas that show up in interviews 🎯 Gotcha #1: Function expressions are NOT hoisted like declarations","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F8.webp",{"local":52},{"headline":55,"body":56,"imageUrl":57,"images":58},"Only the variable greet is hoisted (as undefined)","Only the variable greet is hoisted (as undefined). Calling undefined() gives you a TypeError. With let\u002Fconst you'd get a ReferenceError instead. Gotcha #2: The classic var in a loop","\u002Fapi\u002Fmedia\u002Fposts\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-t-e2879a4e\u002F9.webp",{"local":57},[60],"dev",[62],"Technology",{"name":64,"url":65},"Dev.to","https:\u002F\u002Fdev.to\u002Fsurya_kanth_666\u002Funder-the-hood-of-javascript-demystifying-hoisting-objects-this-and-the-new-keyword-25pa","en",{"handle":68,"displayName":69},"spots","Spots","queued",{"views":72,"likes":73,"saves":73,"shares":73,"completions":73,"opens":73,"skips":73,"depthSum":73},3,0,"2026-09-29T07:57:19.254Z","local"]