Facebook Pixel

Common Closure Interview Questions in JavaScript

Closures are the most important interview topic. Here are the most asked questions with code and answers.

Common Closure Interview Questions in JavaScript

Closures are the most important interview topic in JavaScript. Here are the most common questions, with code and answers.

Q1: What is a closure?

A function that remembers its lexical scope, even after the outer function has returned.

function outer() { const x = 10; return () => x; } const fn = outer(); fn(); // 10 `` ### Q2: What is the output? ```js function makeCounter() { let count = 0; return () => ++count; } const c = makeCounter(); console.log(c(), c(), c()); `` **Answer**: `1 2 3`. The returned function closes over `count` and increments it each call. ### Q3: What is the output? ```js for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: `3 3 3`. `var` is function-scoped; all callbacks share one `i`, which is `3` by the time they run. ### Q4: What is the output? ```js for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } `` **Answer**: `0 1 2`. `let` creates a fresh binding per iteration; each callback closes over its own `i`. ### Q5: What is the output? ```js function createFunctions() { const result = []; for (var i = 0; i < 3; i++) { result.push(() => i); } return result; } const fns = createFunctions(); console.log(fns.map(f => f())); `` **Answer**: `[3, 3, 3]`. All closures share the same `i` (var). Fix with `let` or an IIFE. ### Q6: What is the output? ```js function counter() { let count = 0; return { increment: () => ++count, getCount: () => count, }; } const c = counter(); c.increment(); c.increment(); console.log(c.getCount()); `` **Answer**: `2`. Both methods close over the same `count`. ### Q7: Implement a function that runs only once. ```js function once(fn) { let done = false; let result; return (...args) => { if (done) return result; done = true; result = fn(...args); return result; }; } const init = once(() => { console.log("init"); return 42; }); init(); // logs "init", returns 42 init(); // returns 42 (no log) `` ### Q8: Implement a memoize function. ```js function memoize(fn) { const cache = {}; return (...args) => { const key = JSON.stringify(args); if (!(key in cache)) cache[key] = fn(...args); return cache[key]; }; } `` ### Tips for Interviews - Explain that a closure is a function plus its lexical environment. - Walk through which variables are closed over. - Remember: `var` in loops causes the shared-variable bug; `let` fixes it. - Mention data privacy, memoization, and currying as use cases. - Mention memory: closures keep variables alive (potential leaks). ### The Takeaway Closure interview questions test: the definition (function + environment), the counter pattern, the loop closure bug (`var` vs `let`), data privacy, and utility functions (once, memoize). Explain the mechanism, walk through the closed-over variables, and mention real-world use cases.

A function that remembers the variables from its lexical scope, even after the outer function has returned. The function object holds a reference to its outer lexical environment, so it can access those variables later.

Because var is function-scoped. All callbacks share one i. By the time the callbacks run, the loop has finished and i is 3. Using let fixes this because let creates a fresh binding per iteration, and each callback closes over its own i.

Use a closure with a done flag. The first call sets done to true and stores the result. Subsequent calls return the stored result without running the function again.

Create a cache object in the outer function. Return a function that checks the cache for the arguments (serialized as a key). If not found, call the original function and store the result. Return the cached result.

The definition, the counter pattern (makeCounter), the loop closure bug (var vs let), data privacy via closures, and utility functions like once and memoize. Also: explaining why closed-over variables survive after the outer function returns.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.