1D0-720 · Question #29
What is a closure in JavaScript?
The correct answer is B. A function having access to the parent scope, even after the parent function has closed. Option B is correct because a closure is formed when a function "closes over" variables from its outer (parent) scope, retaining access to those variables even after the parent function has finished executing - this is a core feature of JavaScript's lexical scoping. Option A is…
Question
What is a closure in JavaScript?
Options
- AA type of event handler.
- BA function having access to the parent scope, even after the parent function has closed.
- CA method used to close a web page.
- DAn error in JavaScript code.
How the community answered
(14 responses)- A7% (1)
- B86% (12)
- D7% (1)
Explanation
Option B is correct because a closure is formed when a function "closes over" variables from its outer (parent) scope, retaining access to those variables even after the parent function has finished executing - this is a core feature of JavaScript's lexical scoping. Option A is wrong because event handlers are callbacks assigned to DOM events, a separate concept entirely. Option C is wrong because JavaScript has no built-in mechanism to "close" a web page programmatically in that sense. Option D is wrong because closures are intentional, well-defined behavior, not an error.
Memory tip: Think of a closure like a backpack - when an inner function leaves its parent, it carries the parent's variables with it in that backpack, using them whenever needed later.
Topics
Community Discussion
No community discussion yet for this question.