JavaScript Closures Interview Questions
Closures are the single most-tested JavaScript concept. Here are the questions and clear answers.
What is a closure and why does it exist?
A function bundled with the variables from the scope where it was created — it keeps a live reference to them even after that scope returns. It's a natural result of lexical scope, not a special feature.
What are practical uses of closures?
Private state (the module pattern), function factories, memoization caches, and the timers inside debounce/throttle. Every React hook that 'remembers' a value between renders is a closure.
Why does a var loop with setTimeout print the same number?
var has one binding shared by every iteration, so all callbacks read the final value. let creates a fresh binding per iteration — the fix.
for (let i=0;i<3;i++) setTimeout(()=>console.log(i)); // 0 1 2What is currying?
Transforming f(a,b,c) into a chain of calls — collect arguments until you reach the function's arity, then invoke. It's another closure-driven pattern enabling partial application.
How do closures enable memoization?
A cache (Map) lives in the outer function's scope; the returned function checks it and only computes on a miss — turning repeated expensive calls into O(1) lookups.
How can a closure cause a memory leak?
Captured variables can't be garbage-collected while the closure is alive. Holding closures in long-lived structures (caches, listeners you never remove) keeps everything they captured in memory.
Master closures and scope
Closures, scope, this and the patterns built on them — with clear explanations in the JavaScript Interview Kit.
⚡ Get the JavaScript Interview Kit → ₹299Frequently asked questions
- Why are closures asked so often?
- They reveal whether you understand lexical scope — the foundation of the module pattern, React hooks and many coding patterns.
- How do I explain a closure in an interview?
- 'A function plus the variables it captured from its defining scope,' then show a counter example — it lands every time.
