What is a closure in JavaScript?

Prepare for the TJR Bootcamp Test with quizzes and flashcards. Each question includes hints and explanations to boost your readiness for the exam!

Multiple Choice

What is a closure in JavaScript?

A closure is a function that retains access to its lexical scope even when executed outside that scope. In JavaScript, when a function is created inside another function, it closes over the outer variables, so those bindings stay available even after the outer function has finished.

For example, if an outer function defines a variable and returns an inner function that uses that variable, calling the returned function later will still see the original variable’s value. This happens because the inner function keeps a reference to the outer scope where those variables live, forming a closure.

This isn’t just about returning a function; it’s about the function keeping access to the outer scope’s variables. It’s also not about the global execution context or an array method.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy