What is a closure? Show a practical example.
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
A closure is a function together with access to its surrounding lexical environment. The function can use those variables even after the outer function has retu ed. A counter factory can create independent counters without exposing a writable global variable. Closures also support event handlers and configurable functions. They capture bindings rather than frozen snapshots of every value, so later changes can affect what the function sees. Long-lived closures can keep referenced objects reachable, which matters when investigating memory growth.
Related Questions in JavaScript & Web Technologies
Explain the JavaScript Event Loop, Call Stack, Microtask, and Macrotask Queues
Intermediate level • 429 views
How do let, const, and var differ in scope and reassignment?
Easy level • 6 views
How should fetch code handle HTTP errors and malformed JSON?
Medium level • 6 views
How do event bubbling and delegation help with a dynamic list of buttons?
Medium level • 6 views
What is the output of synchronous code, a resolved promise, and setTimeout with zero delay?
Medium level • 5 views