In this article, we will discuss about the closures working JavaScript. Let us first understand what exactly closures are and basic details which are associated with closures in JavaScript.
A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environment). In JavaScript, closures are created every time a function is created at run time. In other words, a closure is just a fancy name for a function that remembers the external things used inside it.
Let us look at some examples to understand how closures actually work in JavaScript.
Â
Example 1:
- In this example, we will be declaring a closure which would eventually access an outer variable balance from the outer function.
- After using the outer variable in inner most function, that particular closure will help us to deduct 100 from it, each time whenever that outer function is called.
HTML
<!DOCTYPE html> <html> Â Â <body> Â Â Â Â <h2>JavaScript Closures</h2> Â Â Â Â <button type="button" onclick="initaccount()"> Â Â Â Â Â Â Â Â Â Â Click Me! Â Â Â Â Â Â Â Â </button> Â Â Â Â <p id="demo"></p> Â Â Â Â <script> Â Â Â Â function initaccount() { Â Â Â Â Â Â Â Â var balance = 1000; Â Â Â Â Â Â Â Â Â Â function currentbalance() { Â Â Â Â Â Â Â Â Â Â Â Â balance = balance - 100; Â Â Â Â Â Â Â Â Â Â Â Â alert(balance); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â currentbalance(); Â Â Â Â } Â Â Â Â </script> </body> Â Â </html> |
Output:
Explanation: In the above example, currentbalance() can access outer variable balance hence balance is deducted by 100 each time initaccount() method is called.Â
Example 2: Closures can be nested as well as in below example. Here in the example both outerfunction() and innerfunction() has access to counter variable , hence on calling Counter() both outerfunction() and innerfunction() increments the value of counter. In this context, we can say that closures have access to all outer function scopes.
HTML
<!DOCTYPE html> <html> Â Â <body> Â Â Â Â <h2>JavaScript Closures</h2> Â Â Â Â <button type="button" onclick=" Counter()"> Â Â Â Â Â Â Â Â Click Me! Â Â Â Â </button> Â Â Â Â <p id="demo1"></p> Â Â Â Â <p id="demo2"></p> Â Â Â Â <script> Â Â Â Â function Counter() { Â Â Â Â Â Â Â Â var counter = 0; Â Â Â Â Â Â Â Â Â Â function outerfunction() { Â Â Â Â Â Â Â Â Â Â Â Â counter += 1; Â Â Â Â Â Â Â Â Â Â Â Â document.getElementById("demo1").innerHTML Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = "outercounter = " + counter + Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "from outerfunction " ; Â Â Â Â Â Â Â Â Â Â Â Â Â Â function innerfunction() { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â counter += 1; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â document.getElementById("demo2").innerHTML Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = " innercounter = " + counter +Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "from innerfunction "; Â Â Â Â Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â Â Â Â Â innerfunction(); Â Â Â Â Â Â Â Â }; Â Â Â Â Â Â Â Â outerfunction(); Â Â Â Â }; Â Â Â Â </script> </body> Â Â </html> |
Output:

