InterviewBuddy
All interview questions

What is a closure?

Beginner

Answer

A closure is a function together with access to its lexical environment. The function can keep using variables from the scope where it was created, even after that outer function returns.

Explanation

Closures capture bindings, not frozen snapshots of values. They are useful for encapsulation, callbacks, and function factories.

Example

function counter() {
  let n = 0;
  return () => ++n;
}