What are JavaScript closures and practical use cases?
Reported in Deloitte interview loops. Core language concept for frontend and Node.js interviews.
Interview scenario
Often asked in Deloitte technical or coding rounds. Prepare a clear spoken answer plus key trade-offs.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at Deloitte: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.
A closure is created when an inner function retains access to variables from its lexical outer scope even after the outer function returns. This enables private state without classes.
Common uses include function factories, memoization, debouncing, and module encapsulation. Closures are powerful but can capture stale values in async callbacks if dependencies are not handled carefully in frameworks like React.
function counter() {
let count = 0;
return () => ++count;
}
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.