Home

Blog

Understanding JavaScript Closures

Understanding JavaScript Closures

What is a closure?

A closure is a function bundled with the variables in scope where it was defined. Even after the outer function returns, the inner function keeps access to those variables.

That's why a counter factory works β€” each call captures its own count:

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

Closures power data privacy, memoization, and most React hooks.

More posts