JavaScript Hoisting Explained

🚀 JavaScript Simplified Series — Day 32 You used a variable… Before even declaring it 😳 And somehow… it still worked? 🤯 How is that even possible? --- ## 🔥 This is called → Hoisting --- ## 🔹 What is Hoisting? Hoisting means: 👉 JavaScript moves declarations to the **top of their scope** Before execution starts --- ## 🔹 Example (Confusing 😵) ```javascript id="hs1" console.log(x) var x = 10 ``` 👉 Output: undefined 😳 --- ## 🔍 What actually happens? JavaScript internally converts it to: ```javascript id="hs2" var x console.log(x) x = 10 ``` 👉 Declaration upar chali gayi 👉 Value assign baad me hui --- ## 🔹 let & const (Important ⚠️) ```javascript id="hs3" console.log(a) let a = 10 ``` 👉 Error ❌ 📌 Because of **Temporal Dead Zone (TDZ)** --- ## 🔥 What is TDZ? Time between: 👉 Variable declared 👉 Aur initialize hone tak Is duration me access nahi kar sakte --- ## 🔹 Function Hoisting ```javascript id="hs4" greet() function greet() { console.log("Hello") } ``` 👉 Works perfectly ✅ 📌 Functions are fully hoisted --- ## 🔥 Real Life Example Think of a classroom 🏫 👉 Teacher announces names first 👉 Details baad me fill hoti hain 👉 Declaration pehle 👉 Value baad me --- ## 🔥 Simple Summary Hoisting → declarations upar move hoti hain var → undefined milta hai let/const → error (TDZ) function → fully hoisted --- ### 💡 Programming Rule **Always declare variables at the top. Don’t rely on hoisting.** --- If you want to learn JavaScript in a **simple and practical way**, you can follow these YouTube channels: • Rohit NegiHitesh Choudhary (Chai aur Code) --- 📌 Series Progress Day 1 → What is JavaScript Day 2 → Variables & Data Types Day 3 → Type Conversion & Operators Day 4 → Truthy & Falsy + Comparison Operators Day 5 → If Else + Switch + Ternary Day 6 → Loops Day 7 → Break + Continue + Nested Loops Day 8 → Functions Basics Day 9 → Arrow + Default + Rest Parameters Day 10 → Callback & Higher Order Functions Day 11 → Arrays Basics Day 12 → Array Methods Day 13 → Array Iteration Day 14 → Advanced Array Methods Day 15 → Objects Basics Day 16 → Object Methods + this Day 17 → Object Destructuring Day 18 → Spread & Rest Day 19 → Advanced Objects Day 20 → DOM Introduction Day 21 → DOM Selectors Day 22 → DOM Manipulation Day 23 → Events Day 24 → Event Bubbling Day 25 → Event Delegation Day 26 → Async JavaScript Day 27 → Promises Day 28 → Async / Await Day 29 → Fetch API Day 30 → Event Loop Day 31 → Scope Day 32 → Hoisting Day 33 → Closures (Next Post) --- Follow for more 🚀 #JavaScriptSimplified #javascript #webdevelopment #coding #programming #learninpublic #100DaysOfCode #frontenddevelopment #devcommunity #codingjourney #softwaredeveloper #techcommunity #dailylearning #codeeveryday Create image based on this

  • graphical user interface

To view or add a comment, sign in

Explore content categories