JavaScript Closures Explained with a Common Interview Trap

🚀 #100DaysOfCode – Day 2 ⚠️ This one concept breaks most JavaScript interviews: ✨ Closures 👀 Look at this: for (var i = 1; i <= 3; i++) { setTimeout(() => console.log(i), 1000); } ❓ What do you expect? → "1 2 3" 😬 What you get? → "4 4 4" 🧠 What’s happening? Closures don’t copy values… they remember variables. And "var" shares the same scope. ✅ Fix (modern JS) for (let i = 1; i <= 3; i++) { setTimeout(() => console.log(i), 1000); } 💡 In one line: Closure = function + memory of its outer scope 🔥 Why you should care? → React hooks → Event handlers → Almost every serious JS interview #JavaScript #FrontendDeveloper #100DaysOfCode #WebDevelopment

To view or add a comment, sign in

Explore content categories