Closure
A closure is a function that remembers the variables from the scope where it was created, even after that outer function has finished running. The inner function "closes over" those variables and can keep reading and changing them.
Closures are used to keep private state, build function factories, and in callbacks and event handlers that need data from when they were set up. They are central to JavaScript, and Python, Kotlin, Swift and Java lambdas use them too.
function makeCounter() {
let count = 0; // private to this counter
return () => ++count; // the closure keeps count alive
}
const next = makeCounter();
next(); // 1
next(); // 2
