What are Closures in JavaScript?
Learn what JavaScript closures are, how they capture lexical scope, real use cases like private state and counters, plus common closure interview questions.
Expected Interview Answer
A closure is a function that remembers and keeps access to the variables from the scope where it was defined, even after that outer function has finished executing.
Every time a function is created in JavaScript, it captures a reference to its surrounding lexical environment. When you return or pass that inner function elsewhere, it carries those captured variables with it, so it can still read and update them long after the outer function returned. Closures are the mechanism behind data privacy, function factories, memoization, and the callbacks used throughout asynchronous code.
- Enables private state that outside code cannot touch directly
- Powers function factories that build customized functions
- Preserves values for callbacks, timers and event handlers
- Underpins currying, memoization and the module pattern
- Avoids polluting the global scope with helper variables
AI Mentor Explanation
A closure is like a bowler who has memorized a specific batter's weaknesses from the nets. Even after the practice session ends and everyone leaves the ground, the bowler carries that private knowledge into every future match and keeps using it, though no spectator can ever see the notes locked inside their head.
Step-by-Step Explanation
Step 1
Define an outer function
Create a function that declares one or more local variables you want to protect.
Step 2
Create an inner function
Inside it, define a nested function that reads or writes those outer variables.
Step 3
Return or pass the inner function
Expose the inner function so it can be called after the outer one returns.
Step 4
The scope is captured
The inner function keeps a live reference to the outer variables, not a copy.
Step 5
Use the retained state
Each call reads or updates the captured variables, which stay private to the closure.
What Interviewer Expects
- A precise definition tying closures to lexical scope
- Understanding that captured variables are references, not snapshots
- A real use case such as private state or a counter factory
- Awareness of closures in loops and the var vs let pitfall
- How closures relate to callbacks and memory retention
Common Mistakes
- Saying a closure copies variables rather than referencing them
- The classic var-in-a-loop bug where all callbacks share one variable
- Confusing closures with plain nested functions that capture nothing
- Ignoring memory leaks from long-lived closures holding large objects
- Believing closures only exist when a function is returned
Best Answer (HR Friendly)
“A closure is a function that remembers the variables around it even after the code that created it has finished. It is how JavaScript keeps information private and lets small helper functions carry their own little memory with them wherever they are used.”
Code Example
function makeCounter() {
let count = 0; // private, only the closure can touch it
return function () {
count += 1;
return count;
};
}
const next = makeCounter();
console.log(next()); // 1
console.log(next()); // 2
// count is not accessible from the outside — it lives in the closureFollow-up Questions
- How would you use a closure to create private variables?
- Explain the classic closure bug in a for loop and how let fixes it.
- How do closures enable currying and partial application?
- Can closures cause memory leaks, and how would you avoid them?
- What is the difference between lexical scope and dynamic scope?
MCQ Practice
1. A closure gives an inner function access to?
A closure retains access to variables in the scope where the function was defined, even after that scope has exited.
2. In a for loop using var, why do all setTimeout callbacks log the same final value?
var is function scoped, so every callback closes over the same single variable, which holds its final value by the time they run.
3. Which pattern most directly relies on closures?
The module pattern uses a closure to keep helper variables private while exposing only chosen methods.
Flash Cards
What is a closure? — A function bundled with references to the variables from the scope where it was defined.
Are captured variables copies? — No — a closure holds live references, so updates are visible across calls.
One key use of closures? — Private state, e.g. a counter whose variable cannot be accessed from outside.
Why does var break loop closures? — All iterations share one function-scoped variable; let creates a fresh binding per iteration.