Closures in JavaScript: The Secret Backpack of Functions
When you start diving deep into JavaScript, you’ll often hear the term closure. At first, it sounds confusing, but once you understand it, closures become one of the most powerful concepts you can use in real-world coding and interviews.
What is a Closure?
A closure is created when a function “remembers” its lexical scope (the variables around it) even after the outer function has finished executing.
In simple terms:
👉 A closure allows an inner function to access variables of its outer function, even after the outer function is gone.
Example 1: A Basic Closure
function outer() {
let count = 0; // variable inside outer functionfunction inner() {
count++;
console.log(count);
}return inner;
}const counter = outer(); // outer executed, but returns inner
counter(); // 1
counter(); // 2
counter(); // 3✅ Even though outer() has finished execution, the counter function still remembers count.
That’s the magic of a closure!
Why Do We Need Closures?
Closures are useful in many scenarios:
- Data Privacy (Encapsulation)
You can hide variables and only expose what’s needed.
function bankAccount() {
let balance = 1000; // private variable
return {
deposit(amount) {
balance += amount;
console.log(`Deposited: ${amount}, Balance: ${balance}`);
},
withdraw(amount) {
if (amount <= balance) {
balance -= amount;
console.log(`Withdrawn: ${amount}, Balance: ${balance}`);
} else {
console.log("Insufficient funds");
}
}
};
}
const account = bankAccount();
account.deposit(500); // Deposited: 500, Balance: 1500
account.withdraw(200); // Withdrawn: 200, Balance: 1300👉 Here, balance is protected and cannot be directly changed from outside.
- Callbacks and Event Listeners
Closures are heavily used in asynchronous code.
function greet(name) {
return function() {
console.log(`Hello, ${name}!`);
};
}
setTimeout(greet("Krishna"), 2000);
// After 2 seconds: Hello, Krishna!- Function Factories
Generate multiple customised functions.
function multiplier(x) {
return function(y) {
return x * y;
};
}
const double = multiplier(2);
const triple = multiplier(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15🧠 Key Interview Question on Closures
Q: What is a closure in JavaScript?
👉 A closure is a function that remembers and accesses variables from its outer scope, even after that outer scope has finished execution.
Get Harikrishna Bomen’s stories in your inbox
Join Medium for free to get updates from this writer.
Q: Where are closures used in real life?
- Data hiding (private variables)
- Callback functions
- Event handlers
- Function factories
- Memorisation (performance optimisation)
Quick Trick to Remember Closures
Think of closures like a “backpack” 🎒:
- When a function is created, it packs up variables from the outer scope in its backpack.
- Wherever the function goes, it carries that backpack with it, even if the outer function is gone.
Conclusion
Closures are not just theory — they are everywhere in JavaScript.
- They give power to functions.
- They allow private variables.
- They are essential for async programming.
If you master closures, you’re already ahead in cracking JavaScript interviews and writing cleaner, smarter code.



