A closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
Loading
A closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasad RaveendranRank #320 · 7.8K reputationPosted Aug 25, 2023, 5:37 PM EDT
In JavaScript, a closure is a function that retains access to its lexical scope even after its containing function has finished executing. This allows you to maintain private data and create functions with shared state. Closures are commonly used for creating private variables, currying, and implementing various design patterns.
Here's a simple example of how to use closures in JavaScript:
function outerFunction() { let outerValue = 10; function innerFunction() { console.log(outerValue); // Accessing outerValue from the parent scope } return innerFunction; } const closure = outerFunction(); // closure now holds the reference to innerFunction closure(); // This will log: 10In this example, innerFunction is a closure because it has access to the outerValue variable even after outerFunction has finished executing. When outerFunction is called, it defines outerValue and then returns innerFunction. When closure() is invoked, it logs the value of outerValue which is still accessible to innerFunction due to the closure mechanism.
Here's another example showing how closures can be used for data encapsulation and creating private variables:
function createCounter() { let count = 0; return { increment: function() { count++; }, decrement: function() { count--; }, getCount: function() { return count; } }; } const counter = createCounter(); counter.increment(); counter.increment(); console.log(counter.getCount()); // This will log: 2In this example, the createCounter function returns an object with three methods: increment, decrement, and getCount. These methods have access to the count variable, which is only accessible through these methods, creating a private variable and encapsulating the data within a closure.
Closures are a powerful concept in JavaScript and are used extensively in various programming scenarios. They allow for elegant solutions to problems involving encapsulation, data hiding, and creating functions with persistent state.
Mohammad HussainRank #513 · 4.8K reputationPosted Aug 25, 2023, 9:06 AM EDT
function outerFunction(outerValue) { // This inner function has access to outerValue due to closure function innerFunction(innerValue) { console.log("Outer value:", outerValue); console.log("Inner value:", innerValue); } return innerFunction; // Return the inner function } const closureExample = outerFunction("Hello, "); closureExample("world!"); // Outputs: Outer value: Hello, Inner value: world!