What are the most common closure interview questions in JavaScript?
The definition, the counter pattern (makeCounter), the loop closure bug (var vs let), data privacy via closures, and utility functions like once and memoize. Also: explaining why closed-over variables survive after the outer function returns.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Common Closure Interview Questions in JavaScript
A function that remembers the variables from its lexical scope, even after the outer function has returned. The function object holds a reference to its outer lexical environment, so it can access those variables later.
Because var is function-scoped. All callbacks share one i. By the time the callbacks run, the loop has finished and i is 3. Using let fixes this because let creates a fresh binding per iteration, and each callback closes over its own i.
Use a closure with a done flag. The first call sets done to true and stores the result. Subsequent calls return the stored result without running the function again.
Still have questions?
Browse all our FAQs or reach out to our support team
