Interview Question

What are closures?

A closure preserves access to its creation environment.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

A closure is a function together with access to the lexical environment where it was created. • The function can read outer bindings after the outer function has returned. • Each closure can retain a different environment. • Closures support private state, callbacks, and function factories.

Example

Code
function counter() {
  let value = 0;
  return () => ++value;
}
const next = counter();
console.log(next(), next());
Output
1 2

Quick Revision

A closure preserves access to its creation environment.