JavaScript Closures Interview Questions 2026: 26 Q&A
26 JavaScript closures interview questions for 2026 with full answers, lexical scope, the loop closure bug, currying, memoization and predict-the-output snippets. Candidate-reported frontend and full-stack favourites.
on this page§ 09
Last Updated: June 2026 | Level: Freshers to Mid-Level | Read Time: ~15 min
Closures are the concept that most distinguishes confident JavaScript developers in interviews. Candidates report that the definition, the loop closure bug, and practical uses like the module pattern and currying are guaranteed questions. This guide covers 26 closure questions with full answers, runnable code, and predict-the-output traps. Behaviour reflects the official MDN documentation; confirm any scope detail on MDN.
Pair this with JavaScript Interview Questions for Freshers 2026 and JavaScript Output Questions 2026.
Table of Contents
- Fundamentals (Q1 to Q9)
- The Loop Bug and Scope (Q10 to Q16)
- Practical Patterns (Q17 to Q23)
- Advanced (Q24 to Q26)
- Predict the Output
- Quick Revision Table
- Frequently Asked Questions
Fundamentals
Q1. What is a closure? Easy
function outer() {
let count = 0;
return function() { return ++count; };
}
const inc = outer();
inc(); inc(); // 1, then 2
Q2. What is lexical scope? Easy
Q3. How does a closure keep a variable alive? Medium
Q4. What is the difference between scope and closure? Medium
Q5. Can closures cause memory leaks? Hard
Q6. Do closures capture by value or by reference? Hard
Q7. What is an IIFE and how does it relate to closures? Medium
(function() { /* private scope */ })();
Q8. Can a closure modify the captured variable? Easy
Q9. How many closures are created in a function with two inner functions? Hard
The Loop Bug and Scope
Q10. What is the classic closure-in-a-loop bug? Medium
for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3 3 3
for (let i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 0 1 2
Q11. How does let fix the loop bug? Medium
Q12. How would you fix the loop bug without let? Hard
for (var i = 0; i < 3; i++) {
(function(j) { setTimeout(() => console.log(j)); })(i);
}
Q13. What is variable shadowing? Medium
Q14. What is the temporal dead zone? Hard
Q15. Do closures work with block scope? Medium
Q16. What does this snippet print and why? Medium
Practical Patterns
Q17. What is the module pattern? Medium
const counter = (function() {
let c = 0;
return { inc: () => ++c, get: () => c };
})();
Q18. What is currying and how does it use closures? Medium
const add = a => b => a + b;
add(2)(3); // 5
Q19. How does memoization use closures? Medium
function memoize(fn) {
const cache = {};
return x => x in cache ? cache[x] : (cache[x] = fn(x));
}
Q20. How do you implement a debounce with closures? Hard
function debounce(fn, ms) {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => fn(...args), ms);
};
}
Q21. How do closures enable data privacy? Medium
Q22. What is a function factory? Medium
const triple = (n => x => x * n)(3);
triple(5); // 15
Q23. How do once-only functions use closures? Medium
Advanced
Q24. What is the difference between a closure and a regular function? Medium
Q25. How do closures interact with the garbage collector? Hard
Q26. Can two closures share state? Hard
function pair() {
let v = 0;
return [() => ++v, () => v];
}
const [set, get] = pair();
set(); get(); // 1
Predict the Output
Snippet 1
function make() {
let x = 0;
return { inc: () => ++x, val: () => x };
}
const m = make();
m.inc(); m.inc();
console.log(m.val());
Output:
2
Why: Both methods close over the same x, so two increments leave it at 2.
Snippet 2
const fns = [];
for (var i = 0; i < 3; i++) fns.push(() => i);
console.log(fns.map(f => f()));
Output:
[ 3, 3, 3 ]
Why: var shares one binding; all closures read the final i of 3. Use let for [0, 1, 2].
Snippet 3
const add = a => b => a + b;
const add5 = add(5);
console.log(add5(3), add5(10));
Output:
8 15
Why: add5 is a closure capturing a=5; each call supplies b.
Quick Revision Table
| Concept | Key takeaway |
|---|---|
| Closure | function plus captured lexical scope |
| Capture | by reference, not snapshot |
| Loop bug | var shares, let per-iteration |
| Module pattern | IIFE returns public API, hides state |
| Currying | chained single-arg closures |
| Memoization | private cache in closure |
| Leak risk | captured big object stays alive |
Frequently Asked Questions
What is a closure in JavaScript in simple terms?
A closure is a function bundled with references to its surrounding lexical scope, so it can read and update outer variables even after the outer function has returned. Candidates report this is the single most asked JavaScript concept.
Why is the loop closure bug asked so often?
Because it tests whether you understand that var closures share one binding while let closures get a fresh binding per iteration. It is the most common closure trap in interviews.
What are practical uses of closures?
Data privacy with the module pattern, function factories and currying, memoization caches, debounce and throttle helpers, and event handlers that remember state. Interviewers expect at least two real uses.
Do closures capture the value or the variable?
The variable, by reference, not a snapshot of its value. That is exactly why a var-loop closure sees the final value while a let-loop closure sees its own iteration value.
Can closures cause memory leaks?
Yes. A reachable closure keeps its captured variables (and anything they reference) alive. Detach handlers and clear references in long-lived applications to let the garbage collector reclaim them.
Related Articles
- JavaScript Interview Questions for Freshers 2026, the master set
- JavaScript Output Questions 2026, tricky snippets
- JavaScript Async Await Interview Questions 2026, promises and the event loop
- JavaScript Coding Questions With Solutions 2026, solved problems
Confirm any scope or closure detail on the official MDN documentation before your interview. This guide reflects candidate-reported patterns and public preparation resources as of June 2026.
Sources and review notesreviewed 8 Jun 2026
Official notices, candidate reports, offer documents, and editorial practice questions carry different confidence levels. The visible source list lets you inspect the evidence instead of relying on a blanket verification badge.
topic cluster
More resources in Interview Questions
Use the category hub to browse similar questions, exam patterns, salary guides, and preparation resources related to this topic.
paid contributor programme
Sat this this year? Share your story, earn ₹500.
First-person experience reports help future candidates prep smarter. We pay verified contributors ₹500 via UPI per accepted story with byline.
Submit your story →ready to practice?
Take a free timed mock test
Put what you learned into practice. Our mock tests match the 2026 pattern with timer, navigator, reveal, and score breakdown. No signup.