100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What Is a Closure in JavaScript?

Learn what a JavaScript closure is, how the scope chain keeps variables alive, and common closure pitfalls with examples.

mediumQ25 of 224 in Web Development Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A closure is a function bundled together with references to the variables from its enclosing lexical scope, so the function keeps access to those variables even after the outer function has finished executing.

When a function is defined inside another function, JavaScript does not copy the outer variables into the inner function — it keeps a live reference to the enclosing scope through the scope chain. That means the inner function can still read and mutate those variables long after the outer function has returned, because the JavaScript engine keeps that scope alive in memory as long as something references it. This is how patterns like private counters, memoization caches, and event-handler state work without any global variables. The tradeoff is memory: variables captured by a closure are not garbage collected while the closure itself is reachable, so long-lived closures over large objects can leak memory if not managed carefully.

  • Enables true data privacy without classes or global state
  • Powers common patterns like memoization, currying, and debouncing
  • Lets callbacks and event handlers retain context across async boundaries
  • Avoids polluting the global namespace with helper variables

AI Mentor Explanation

A closure is like a bowler who memorizes the exact field placement called out before the over even after the captain who set it walks off the ground. The bowler keeps bowling to that specific plan every delivery, referencing the same field positions, even though the conversation that created the plan is long over. Nobody else on the team can see or change that private field memory unless the bowler exposes it. That persistent, private reference to a now-finished setup is exactly what a closure keeps alive around a function.

Step-by-Step Explanation

  1. Step 1

    Outer function defines variables

    A variable is declared inside the outer function’s local scope.

  2. Step 2

    Inner function is defined and returned

    A nested function referencing that variable is created and returned or passed out.

  3. Step 3

    Outer function finishes executing

    The outer function returns, but its scope stays alive because the inner function still references it.

  4. Step 4

    Inner function is invoked later

    Calling the returned function reads or mutates the still-alive captured variable via the scope chain.

What Interviewer Expects

  • Clear explanation of lexical scoping and the scope chain
  • A concrete example like a counter factory or memoization cache
  • Understanding that closures capture variables by reference, not by value
  • Awareness of memory implications for long-lived closures

Common Mistakes

  • Claiming closures copy variable values instead of capturing live references
  • Confusing closures with simple callback functions in general
  • Forgetting the classic loop-with-var bug where all closures share one variable
  • Not mentioning any practical use case like private state or debouncing

Best Answer (HR Friendly)

A closure is when a function remembers and can still use variables from the place it was created, even after that outer code has already finished running. It is how JavaScript lets you build things like private counters or functions that remember settings from earlier.

Code Example

A counter factory using a closure
function createCounter() {
  let count = 0 // captured by the returned function
  return function increment() {
    count += 1
    return count
  }
}

const counterA = createCounter()
console.log(counterA()) // 1
console.log(counterA()) // 2

const counterB = createCounter()
console.log(counterB()) // 1 - independent closure, separate scope

Follow-up Questions

  • Why does using var instead of let inside a loop break closures in event handlers?
  • How would you implement a memoization function using a closure?
  • What is the memory cost of holding onto closures that capture large objects?
  • How do closures relate to the module pattern for private state?

MCQ Practice

1. What does a closure allow a function to do?

A closure keeps a live reference to its enclosing lexical scope after the outer function returns.

2. In the classic `for (var i ...)` closure bug, what value do all the created callbacks see?

var is function-scoped, so every closure in the loop shares one variable, which ends at its final value.

3. Why can closures cause memory leaks?

As long as a closure is reachable, the scope it captured stays in memory, which can leak large captured objects.

Flash Cards

What is a closure?A function bundled with a live reference to its enclosing lexical scope.

Are closure variables copied or referenced?Referenced live, not copied by value.

Classic closure bug with var in loops?All closures share the same function-scoped variable, not per-iteration copies.

One practical use of closures?Private state, e.g. a counter factory or memoization cache.

1 / 4

Continue Learning