closures pracitse:

Here’s a curated list of 20 closure-related JavaScript questions, progressing from easy to hard, covering fundamental to advanced concepts:
Easy (1-7) - Basic Understanding
What is a closure? (Definition)
Write a function
counter()
that returns a function which increments and returns a count each time it's called.const count = counter(); count(); // 1 count(); // 2
Fix the loop to print
1, 2, 3
after 1s each:for (var i = 1; i <= 3; i++) { setTimeout(() => console.log(i), 1000); }
Predict the output:
function outer() { let x = 10; function inner() { console.log(x); } return inner; } const foo = outer(); foo(); // ?
Create a private counter using closures where the variable can't be accessed directly.
What will this log?
let a = 1; function outer() { let a = 2; function inner() { console.log(a); } return inner; } const func = outer(); func(); // ?
Write a function
multiplier(x)
that returns a function to multiply its argument byx
.const double = multiplier(2); double(5); // 10
Medium (8-14) - Practical Applications
Memoization: Create a
memoize(func)
that caches results of expensive function calls.Function Factory: Write a
createGreeting(greeting)
that returns a function to greet a name.js
Copy
Download
const sayHi = createGreeting("Hi"); sayHi("Alice"); // "Hi, Alice"
Currying: Convert
add(a, b, c)
intoadd(a)(b)(c)
.Predict the output:
js
Copy
Download
function outer() { let count = 0; return function inner() { count++; return count; }; } const c1 = outer(); const c2 = outer(); console.log(c1(), c1(), c2()); // ?
Debouncing: Implement
debounce(func, delay)
to delay function execution.Throttling: Implement
throttle(func, limit)
to restrict function calls.Module Pattern: Create a
calculator
module with privatetotal
and methodsadd
,subtract
,getTotal
.
Hard (15-20) - Advanced Scenarios
Partial Application: Write a
partial(func, ...args)
that fixes some arguments.Closure Chain: Predict the output:
function outer() { let x = 1; return function middle() { let y = 2; return function inner() { console.log(x + y); }; }; } outer()()(); // ?
Closure + Async: Fix this to log
0, 1, 2
:js
Copy
Download
for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); } ``` *(Using closures)*
Closure in Event Listeners: Why does this log the last
i
? Fix it.js
Copy
Download
for (var i = 0; i < 3; i++) { document.getElementById(`btn-${i}`).addEventListener('click', () => console.log(i)); }
Closure + Prototypes: Explain how closures differ from class private fields (
#privateVar
).Closure Leak: How can closures cause memory leaks? Give an example.
Bonus (Expert Level)
Implement
once(func)
(allows function to run only once).Closure + Generators: Create a closure-based iterator.
Closures in React: How do hooks like
useState
use closures?
These questions cover variable scope, encapsulation, functional programming, optimization (debouncing/throttling), and memory management.
Subscribe to my newsletter
Read articles from Vishal Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
