A closure happens when a function keeps access to variables from its outer scope, even after that outer function has finished. It’s how JavaScript allows private data and persistent state inside functions.
Explain Closures in JavaScript
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
The Core Idea
A closure is when a function remembers variables from the place where it was created — not from where it is called. It means inner functions can keep using values from an outer function, even after that outer function has finished running.
// Simple example
function makeCounter() {
let count = 0; // stays in memory
return function () {
count++;
return count;
};
}
const counter = makeCounter();
console.log(counter()); // 1
console.log(counter()); // 2
// The inner function still 'remembers' count
Why It’s Useful
Closures are powerful because they let you:
- Keep variables private (not accessible globally)
- Store state between function calls
- Create helpers like
once(),memoize(), and other reusable patterns
// Example: once()
function once(fn) {
let called = false;
let value;
return function (...args) {
if (!called) {
called = true;
value = fn.apply(this, args);
}
return value;
};
}
const init = once(() => console.log('Initialized'));
init(); // Logs once
init(); // Does nothing — remembers state
Common Mistake
Using var in loops creates one shared variable for all iterations — so all functions close over the same value:
const funcs = [];
for (var i = 0; i < 3; i++) {
funcs.push(() => i);
}
console.log(funcs.map(fn => fn())); // [3, 3, 3]
Fixes:
- Use
letfor block scoping - Or use an IIFE (immediately invoked function) to capture a copy
// Fix with let
for (let j = 0; j < 3; j++) {
setTimeout(() => console.log(j), 0);
}
// 0 1 2
Memory Notes
Closures keep their referenced variables alive as long as the inner function exists. This can accidentally hold big objects or DOM elements in memory, so always clean up unused references.
Imagine you leave a room (outer function ends), but you gave your friend (inner function) a copy of the key to your drawer (variables). Even though you’re gone, they can still open it — that’s a closure!
- Functions automatically form closures in JavaScript.
- Inner functions remember outer variables (their lexical scope).
- Used for data privacy, persistence, and modular code.
- Use
let/constto avoid loop bugs. - Don’t overuse closures in long-lived objects to prevent memory leaks.