Skip to main content
All interview theory questions
closuresmedium

What is a closure, and where would you actually use one?

Answer

A closure is a function that keeps access to the variables of the scope it was defined in, even after that outer function has returned. Practically it's how you get private state: a counter that owns its own count, a debounce that remembers its timer, or a factory that bakes in configuration. The classic bug it explains is a var loop — all the handlers close over one shared binding, so they all report the final value.

Points an interviewer listens for

  • Function + the scope it was created in, retained after return
  • Gives private state without a class
  • Powers debounce/throttle, memoisation, factories
  • Explains the var-in-a-loop bug; let gives a per-iteration binding

Reported at

Similar questions have been reported by candidates at Deloitte, Neerx Technovation, Photon Technologies, TO THE NEW, Zepto, DotStark Technologies.

Now try answering it out loud

Interview practice mode times your spoken answer and checks it against the points above.

Practise this question

More closures questions