Technical Entry Level

Explain closures in JavaScript. Give a practical example of where closures are essential and one where they can cause problems.

Quick Tip

Show the concept and the gotcha: "Closures power the module pattern for encapsulation. The classic pitfall is capturing a loop variable with var — each closure shares the same reference. Using let creates a new binding per iteration."

What good answers include

A closure is a function that retains access to variables from its outer lexical scope even after the outer function has returned. Essential uses: data privacy (module pattern), factory functions, callbacks and event handlers, partial application. Problems: unintended variable capture in loops (the classic var-in-for-loop bug, fixed by let or IIFE), memory leaks when closures hold references to large objects or DOM elements that should be garbage collected.

What interviewers are looking for

Fundamental JavaScript concept. Candidates who cannot explain closures have a gap in language understanding. Ask them to solve the classic loop closure problem to test practical understanding.

← All JavaScript questions