What Is a Closure in JavaScript?
A closure is a function that remembers its lexical scope. Here is the simplest explanation with examples.
What Is a Closure in JavaScript?
A closure is a function that remembers the variables from its lexical scope, even after that scope has finished executing. It "closes over" those variables.
The Simplest Example
function outer() { const message = "hello"; function inner() { console.log(message); } return inner; } const fn = outer(); fn(); // "hello" `` `outer` runs, creates `message`, returns `inner`, and its execution context is destroyed. But `fn()` still logs `"hello"`. How? `inner` keeps a reference to `outer`'s lexical environment. That reference is the closure. ### A Function Plus Its Environment A closure is not just the function. It is the function **plus** the variables it closes over. The function object holds a hidden reference to its outer lexical environment. ### Why Closures Work JavaScript uses lexical scope. The scope chain is determined by where functions are **written**, not where they are **called**. When `inner` is defined inside `outer`, its outer reference points to `outer`'s environment. That reference travels with the function object. ### A Counter Example ```js function makeCounter() { let count = 0; return () => ++count; } const counter = makeCounter(); counter(); // 1 counter(); // 2 counter(); // 3 `` Each call to `counter` increments the closed-over `count`. `count` is private: it cannot be accessed directly, only through the returned function. ### Closures Are Everywhere - **Callbacks**: `setTimeout(() => console.log(x), 0)` closes over `x`. - **Event handlers**: a handler closes over variables from where it was defined. - **Higher-order functions**: `map`, `filter`, `reduce` callbacks close over outer variables. - **Module pattern**: an IIFE returns an object whose methods close over private variables. ### The Takeaway A closure is a function that remembers its lexical scope. The function object holds a reference to its outer lexical environment, so it can access those variables even after the outer function has returned. Closures power data privacy, callbacks, event handlers, and the module pattern.
A function that remembers the variables from its lexical scope, even after that scope has finished executing. The function object holds a reference to its outer lexical environment, so it can access those variables later.
Lexical scope means the scope chain is fixed at write time. When a function is defined inside another, it gets an outer reference to the outer function's environment. That reference travels with the function object, even after the outer function returns.
No. A closure is the combination of a function and its lexical environment (the variables it closes over). The function alone is not the closure; the function plus the retained outer variables is the closure.
Because the inner function holds a reference to the outer lexical environment. As long as the inner function exists, the environment (and its variables) cannot be garbage collected. The scope chain persists.
Callbacks (setTimeout, event handlers), higher-order functions (map, filter, reduce), the module pattern (data privacy), currying, memoization, and any function that returns another function.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

