# JavaScript Closures Explained
A closure is a function that remembers the variables from the scope it was created in, even after that outer scope has finished executing. It's one of the most asked-about JavaScript interview topics because it's genuinely used everywhere, often without developers realizing it.
## A Simple Example
```javascript
function makeCounter() {
let count = 0;
return function () {
count += 1;
return count;
};
}
const counter = makeCounter();
counter(); // 1
counter(); // 2
```
The inner function keeps access to `count` even after `makeCounter` has returned — that's the closure.
## Why It Works
JavaScript functions carry a reference to their lexical scope, not a snapshot of it. As long as the inner function exists, the variables it depends on stay alive in memory.
## Practical Uses
Closures power private variables (data that can't be accessed directly from outside), event handlers that need to remember specific data, and function factories like the counter example above. They're also the foundation of how React hooks like `useState` work internally.
## A Common Pitfall
Closures inside loops using `var` often confuse beginners, since `var` isn't block-scoped:
```javascript
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100); // logs 3, 3, 3
}
```
Switching to `let` fixes this, since `let` creates a new binding per iteration.
## Conclusion
Closures aren't a special syntax — they're just a natural consequence of how JavaScript scoping works. Understanding them makes concepts like hooks, debouncing, and memoization click much faster.
Back to Blogs
JavaScript Closures Explained
A clear, practical explanation of closures in JavaScript — what they are, why they matter, and where you'll actually use them.
14 Aug 2026
6 min read