πŸ” What is Closure in JavaScript? ? Explained with Simple Examples

Image
In JavaScript, a closure is created when a function "remembers" the variables from its outer scope even after the outer function has finished executing . This concept allows functions to access variables from an enclosing scope or function — even after that outer function has returned. Closures are a powerful feature in JavaScript that enable data encapsulation, callback handling, and the creation of private variables. πŸ’‘ Let's Understand with an Example function outerFunction() { let outerVariable = "I am from outer scope!"; function innerFunction() { console.log(outerVariable); // Accessing variable from outer scope } return innerFunction; } const closureFunc = outerFunction(); closureFunc(); // Output: I am from outer scope! πŸ” Explanation: outerFunction defines a variable outerVariable . innerFunction is declared inside outerFunction and uses that variable. Even after outerFunction() has finished executing, innerFunc...

About Us

Hi! I'm Ankur Saini, a passionate web developer and content creator. I started "Web Coding with Ankur" to share everything I learn about building modern websites and applications using technologies like React, JavaScript, Node.js, and more.

My mission is to make web development simple, engaging, and accessible to everyone—whether you're just starting or brushing up your skills.

Feel free to connect with me on:

© 2025 Web Coding with Ankur

Comments

Popular posts from this blog

πŸš€ “JavaScript Debounce Made Simple + Live Example!”

What is Hoisting in JavaScript? πŸ”„ Explained with Example

🧩 Event Delegation in JavaScript – Write Cleaner, More Efficient Code