closures pracitse:

Vishal PandeyVishal Pandey
3 min read

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

  1. What is a closure? (Definition)

  2. 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
    
  3. Fix the loop to print 1, 2, 3 after 1s each:

     for (var i = 1; i <= 3; i++) {
       setTimeout(() => console.log(i), 1000);
     }
    
  4. Predict the output:

     function outer() {
       let x = 10;
       function inner() {
         console.log(x);
       }
       return inner;
     }
     const foo = outer();
     foo(); // ?
    
  5. Create a private counter using closures where the variable can't be accessed directly.

  6. What will this log?

     let a = 1;
     function outer() {
       let a = 2;
       function inner() {
         console.log(a);
       }
       return inner;
     }
     const func = outer();
     func(); // ?
    
  7. Write a function multiplier(x)that returns a function to multiply its argument by x.

     const double = multiplier(2);
     double(5); // 10
    

Medium (8-14) - Practical Applications

  1. Memoization: Create a memoize(func) that caches results of expensive function calls.

  2. 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"
    
  3. Currying: Convert add(a, b, c) into add(a)(b)(c).

  4. 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()); // ?
    
  5. Debouncing: Implement debounce(func, delay) to delay function execution.

  6. Throttling: Implement throttle(func, limit) to restrict function calls.

  7. Module Pattern: Create a calculator module with private total and methods add, subtract, getTotal.


Hard (15-20) - Advanced Scenarios

  1. Partial Application: Write a partial(func, ...args) that fixes some arguments.

  2. Closure Chain: Predict the output:

    function outer() {
      let x = 1;
      return function middle() {
        let y = 2;
        return function inner() {
          console.log(x + y);
        };
      };
    }
    outer()()(); // ?
    
  3. 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)*
    
  4. 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));
    }
    
  5. Closure + Prototypes: Explain how closures differ from class private fields (#privateVar).

  6. 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.

0
Subscribe to my newsletter

Read articles from Vishal Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Pandey
Vishal Pandey