Closure
noun
say it: “KLOH-zher” /ˈkloʊʒər/
A function together with the variables from the place it was created, which it still remembers and can use later.
What is Closure in JavaScript?
Closure — An inner function that remembers the variables around it.
function createCounter() {
let count = 0;
return () => ++count;
}
In a sentence
- “The click counter works because of a closure: the inner function still remembers count after the setup function has returned.”
- “The counter function is a closure: it still remembers tokens long after makeCounter returned.”
- “The click counter works because of a closure: the function it returns still remembers count.”
- “My debounce helper uses a closure to keep track of the timer between calls.”
Level: Journeyman · More JavaScript terms at this level
Updated