A Closure is one of the most powerful and fundamental concepts in JavaScript. A closure is the combination of a function bundled together with references to its surrounding lexical state (the lexical environment). In simple terms: a closure gives a function access to its outer scope even after the outer function has finished executing!
Normally, when a function finishes executing, its local variables are removed from the Call Stack and garbage collected.
However, if an inner function references variables from the outer function, JavaScript preserves the outer scope in memory as long as the inner function is alive:
JavaScript historically did not have private class fields. Closures provided the standard way to create private, encapsulated state:
A closure allows you to create customizable function generators:
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Rest & Spread Operators | Immediately Invoked Functions (IIFE) |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.