Closures in JavaScript
Fast recall
Quick revision
Core answer
Whether you understand lexical scope and callback behavior.
Saying a closure copies values rather than retaining access to the environment.
Connect closures to event handlers and explain how long-lived closures can retain memory.
Interview-ready guide
Interview answer
A closure is a function with access to variables from its lexical outer scope. That scope stays alive while the function is reachable, which is why closures are useful for private state and callbacks but can retain memory if listeners are never cleaned up.
Closures in JavaScript
Direct Answer
A closure is a function bundled with references to variables from its lexical outer scope. It can continue to read or update those variables after the outer function has returned, which enables factories, private state, and callbacks.
Deep Dive & Architecture
A closure is a function bundled with references to variables from its lexical outer scope.
It can continue to read or update those variables after the outer function has returned, which enables factories, private state, and callbacks.
A closure keeps access to the lexical environment where it was created. The outer function may finish, but the captured binding remains reachable through the inner function.
Saying a closure copies values rather than retaining access to the environment.
Code Example
function makeCounter() {
let count = 0;
return () => ++count;
}
const next = makeCounter();
next(); // 1
next(); // 2
Common Mistakes & Pitfalls
Saying a closure copies values rather than retaining access to the environment.
Check your understanding
Answer these without looking back at the article.
- When would you use Functions And Closures in a real project?
- How would you compare Functions And Closures with an alternative?
- What is a common mistake when using Functions And Closures?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.