What are JavaScript closures?

Closures happen when an inner function accesses a variable from an outer function, even after that outer function has finished executing. This is essential for data encapsulation.

function outer() {
  let count = 0;
  return function() {
    count++;
    console.log(count);
  };
}
const counter = outer();
counter(); // 1
counter(); // 2