Skip to content
FrontDepth, Home

Back to the topic

Practice: Closures

Answer first, then compare. Rate yourself honestly — it drives your weak-areas list.

4 predict-output · 4 open

Questions up to level

Shows questions up to the selected level.

No practice questions for this level yet.

  1. Predict the outputJunior

    Two counters are created by the same factory. What does this code log?

    JavaScript
    function createCounter() {
      let count = 0;
      return function () {
        count += 1;
        return count;
      };
    }
    
    const a = createCounter();
    const b = createCounter();
    
    console.log(a());
    console.log(a());
    console.log(b());
    console.log(a());
  2. Open questionJunior

    What is a closure in JavaScript? Give an example of where you would use one.

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  3. Predict the outputMiddle

    Two loops schedule timers, one with var and one with let. What does this code log?

    JavaScript
    for (var i = 0; i < 3; i++) {
      setTimeout(() => console.log("var " + i), 0);
    }
    
    for (let j = 0; j < 3; j++) {
      setTimeout(() => console.log("let " + j), 0);
    }
  4. Predict the outputMiddle

    Two closures read a variable that is reassigned later. What does this code log?

    JavaScript
    let x = 1;
    const getX = () => x;
    const setX = (value) => {
      x = value;
    };
    
    function snapshot(value) {
      return () => value;
    }
    
    x = 2;
    console.log(getX());
    
    const frozen = snapshot(x);
    setX(3);
    console.log(getX());
    console.log(frozen());
  5. Open questionMiddle

    How does the engine make closures work, and why does a closure see later changes to a variable?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  6. Predict the outputSenior

    This plain-JS model mimics three React renders and an effect that is created only once. What does it log?

    JavaScript
    let state = 0;
    const ref = { current: 0 };
    let staleEffect = null;
    let refEffect = null;
    
    function Counter() {
      const count = state;
      ref.current = count;
      if (staleEffect === null) {
        staleEffect = () => console.log("stale: " + count);
        refEffect = () => console.log("ref: " + ref.current);
      }
      return () => console.log("latest: " + count);
    }
    
    let onClick = Counter();
    state = 1;
    onClick = Counter();
    state = 2;
    onClick = Counter();
    
    staleEffect();
    refEffect();
    onClick();
  7. Open questionSenior

    How can closures cause memory leaks, and how do you prevent and find them?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.

  8. Open questionSenior

    What is a stale closure in React, and how do you fix it?

    Think it through first

    Answer out loud or in your head as if you were in an interview, then reveal.