Understanding Closures in JavaScript with a Bollywood Twist

Closures in JavaScript — Full Bollywood Vibes Edition 🍿 In JavaScript, Closure is like that mystery character in a movie — quiet at first, but once you understand it, you realize, “picture toh ab samajh mein aayi!” 😎 Let’s break it down with some filmy style 👇 --- 💡 What is a Closure? Closure is when a function remembers its lexical scope — even after the outer function has finished execution. Or simply put: > “Jo baat andar hoti hai, wo andar hi rehti hai!” (— Gangs of Wasseypur vibes) 😜 Example 👇 function makeHero(name) { return function dialogue() { console.log(`${name} — kabhi kabhi lagta hai apun hi bhagwan hai!`); }; } const hero = makeHero("Hemant"); hero(); // Hemant — kabhi kabhi lagta hai apun hi bhagwan hai! Even after makeHero finishes, the inner function still remembers name. That memory = Closure! 🧠 --- 🎭 Closures Are Everywhere! When you use: setTimeout event listeners callback functions or React hooks (useState/useEffect) Closures are silently doing their work behind the scenes! Just like a background character holding the movie together without getting the spotlight 🎥 > “Main hoon na… bas dikhta nahi hoon.” (— SRK style) 😅 --- ⚙️ Another Example — Private Variables Closures help you create private data 👇 function bankAccount() { let balance = 1000; return { deposit(amount) { balance += amount; console.log(`Balance: ₹${balance}`); }, withdraw(amount) { balance -= amount; console.log(`Balance: ₹${balance}`); }, }; } const myAcc = bankAccount(); myAcc.deposit(500); // ₹1500 myAcc.withdraw(200); // ₹1300 Here, balance can’t be accessed directly — it’s safe inside the closure 🔒 > “Jo andar gaya, wo bahar nahi aata.” (— Hera Pheri energy 💸) --- 💬 In short: Closures = Memory + Power They let your inner functions remember data long after the scene is over. Or as our hero would say: > “Don ko pakadna mushkil hi nahi… impossible hai!” — And same goes for understanding JS deeply without knowing closures 😉 --- #JavaScript #Closures #WebDevelopment #CodingHumor #Frontend #LearningIsFun #Bollywood

  • No alternative text description for this image

Like and Share #interview #javascript

Like
Reply

To view or add a comment, sign in

Explore content categories