We may earn an affiliate commission when you visit our partners.

Closure

Save
May 1, 2024 4 minute read

Closure in the context of programming refers to the ability of a function to remember the variables it had access to at the time of its creation. This means that the variables persist within the function's scope even after the execution of the function has completed. Closures are prevalent in various programming languages and notably in JavaScript. In JavaScript, closures can be created when a function is declared within another function, referred to as its parent function. The inner function obtains access to the variables in the scope of its parent function, giving rise to a closure. In essence, the inner function forms a closure around the variables in its parent function's scope.

Function Invocation and Closure

The invocation of a function creates a unique execution context which establishes the function's own scope. However, if the function includes a nested function, the nested function's scope is enclosed within the parent function's scope. When the nested function accesses variables declared in the parent function, they are stored in the closure. Consequently, the variables continue to exist even after the parent function has completed execution.

To illustrate this, consider the following function in JavaScript:

function outerFunction() { let variableInParentFunction = 'Value'; function innerFunction() { console.log(variableInParentFunction); } return innerFunction; } const innerFunctionReference = outerFunction(); innerFunctionReference(); // Logs 'Value'

Share

Help others find this page about Closure: by sharing it with your friends and followers:

Reading list

We've selected 12 books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Closure.
Covers closures in JavaScript in depth, including advanced topics such as scope chains and the 'this' keyword.
Introduces functional programming in JavaScript, covering closures and other related topics.
Provides a comprehensive introduction to JavaScript, including a chapter on closures and scope.
Presents a collection of practical tips and techniques for writing effective JavaScript code, including a chapter on closures.
Provides a concise introduction to closures, covering the basics of how they work and how to use them.
Provides a comprehensive overview of closures in Ruby, covering both the theoretical foundations and practical applications.
Table of Contents
Our mission

OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.

Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.

Find this site helpful? Tell a friend about us.

Affiliate disclosure

We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.

Your purchases help us maintain our catalog and keep our servers humming without ads.

Thank you for supporting OpenCourser.

© 2016 - 2025 OpenCourser