Machine-translated by Google. Questions are authored in English.
Top

Discussion

In JavaScript, what is a closure ?

  • A.A function that returns nothing
  • B.A function bundled with its lexical environment
  • C.A loop termination condition
  • D.An error handling mechanism

Answer: B

A closure is a function bundled with its lexical environment.

When a function is defined inside another function, it keeps access to the outer function's variables even after the outer function has returned. For example, function counter() { let c = 0; return () => ++c; } returns a function that remembers and updates its own c. Closures are used for data privacy, callbacks and function factories.

A function that returns nothing simply returns undefined. Loop termination conditions and error handling (try/catch) are unrelated.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.