Understand Closure in Javascript
Closure is a very important topic to understand in Javascript. Let's understand what is closure and what can be done using this.
In most of the programming languages, if you create a function, you pass parameter or you define a variable inside the function.
var sum = function (num2) {
var num1 = 1;
return num1+ num2;
}
console.log(sum(2)); // 3In Javascript, we can get the same by without passing the parameter also.
var num2 = 2;
var sum = function () {
var num1 = 1;
return num1+ num2;
}
console.log(sum()); // 3This is as simple as declaring the variable globally. But where is closure here? Yes, this is closure. This is a simple example of closure. I will show you an example that how it works and then I will show you another way of use.
Example 1
In Javascript, a variable defined outside the function is accessible from inside the function. This is possible because of something called Lexical Scoping in Javascript means anything defined outside can be accessed from inside of function but an inner variable can’t be accessed from outside function.
But how does it happen? And the answer is closure.
Note: We are using Chrome and console. dir() is the way to see all the properties of a specified JavaScript object in the console by which the developer can easily get the properties of the object.
Get Parag Poddar’s stories in your inbox
Join Medium for free to get updates from this writer.
In the above image after running the function, we can see the property num2 and value is equal to 2 in Closure object. The function is using the variable which is defined outside the function using Closure. When we call the sum function, it looks through the variable inside. If it is not defined inside the function, it looks at the scope outside the function and keeps searching until it finds it and if it can’t find, the value will be undefined. And if it finds, it will add it with num1.
Example 2
So the previous example was very simple. Earlier I told there is another way of the use of closure by using an inner function. Technically, any function where we use a variable from outside the scope is actually closure.
From the above images, we can understand that it preserves the value inside as a property Closure and then when we execute the returned anonymous function, it uses the Closure to do the calculation. So closure is nothing but a function with preserve data.
Example 3
In this example, I am going to show you you can emulate private properties that are found in other programming languages. Private properties are useful to restrict access to code.
function bankAccount(initialBalance) {
var balance = initialBalance;
return {
getBalance: function() {
return balance;
}
}
}var account = bankAccount(10000);console.log(account.getBalance()); // 10000
In this example, we can’t access balance from outside of the bankAccount function. This means we have created a private variable. We can get the balance by only if we call account.getBalance(). So this the power of closure here where a function remembers its lexical scope (compile-time scope), even when the function is executed outside that lexical scope.
Conclusion
I hope that it has cleared a basic understanding of Closure to you!!










