JavaScript · The language round

What is a closure in JavaScript?

Mediumscopelexical environmentfunctionsUpdated Oct 2026
Quiz this track

Short answer

A closure is a function that keeps a live reference to the variables in the scope where it was created, even after the outer function has returned.

A closure is not special syntax. It is what happens when a function is defined inside another function or block and that inner function is used after the outer scope has finished executing. JavaScript uses lexical scoping: a function's variable lookup is decided by where it is written, not where it is called. Because the inner function still references the outer binding, the engine keeps that environment alive instead of discarding it.

js
function createCounter(start = 0) {
  let count = start;
  return {
    increment: () => ++count,
    read: () => count,
  };
}

const counter = createCounter(10);
counter.increment();
counter.increment();
console.log(counter.read()); // 12
The returned functions close over the same count binding.

count is not exposed on the returned object and cannot be reached from outside. The only way in is through the two functions, which share the same captured binding.

Where closures show up

  • Private state and the module pattern
  • Event handlers and callbacks that need context from where they were created
  • Memoization caches and factories such as once(), debounce() or throttle()
  • React components, where each render closes over that render's props and state

The classic follow-up is the loop gotcha. A var loop has one shared binding, so every callback sees the final value. let creates a fresh binding per iteration.

js
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0); // 3, 3, 3
}

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0); // 0, 1, 2
}

Common mistakes

  • "A closure copies the value when it is created." It captures the binding, so later mutations are visible.
  • "Closures only exist in arrow functions." Any function can close over its lexical scope.
  • "Closures keep the entire outer scope alive." In practice they retain what is referenced; engines optimise the rest.
  • Confusing closure with this. A closure captures variables; this is a separate binding decided at the call site.
Closures are not automatically a memory leak. They become a problem when a long-lived callback keeps a large object reachable that you no longer need.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why does a `var` loop print the same value three times?
  2. 2Can closures cause memory leaks?
  3. 3How do you fix a stale closure in React?
  4. 4What is the difference between a closure and `this`?
  5. 5Implement `once(fn)` or `memoize(fn)` using a closure.

Say it in the interview

A closure is a function plus the lexical environment it was created in. When I return an inner function, it keeps a reference to the outer function's variables, so they stay alive after the outer call returns. I use closures for private state, callbacks, memoization and factories. A common pitfall is capturing a `var` loop variable, where every callback sees the final value; using `let` gives each iteration its own binding.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live JavaScript mock interview and get scored feedback on depth, clarity and confidence.