Thursday, September 7, 2017

Javascript Closure | Simply Explained

In Javascript world the things are always confused. The most confused and made complicated feature is closure. But the truth is

Javascript Closure is really Simple

Let me start with the simple below javascript function

function simpleClosure(passed) {
   var inner = 20;
   return inner + passed;
}

simpleClosure(10);

The above function is the simple example for closure. 

When simpleClosure function executed, the passed(argument) value will be added in simpleClosure lexical scope.

Below code will give you better understanding.

var addTen = simpleClosure(10);
console.log(addTen); // Outputs 10

I am going to do the simple change in simpleClosure function. I moved passed argument variable to outer scope.

var passed = 10;
function simpleClosure() {
var inner = 20;
return inner + passed;
}
console.dir(simpleClosure);


Please go through the below output in the console, where you can see passed value 10 is under closure.



Lets see a bit complex closure, where inner function is involved.

function mediumClosure(passed) {
   return function () {
     var inner = 20;
     return inner + passed;
   }


var addTen = mediumClosure(10);

var addTwenty =  mediumClosure(20);
console.dir(addTen);
console.dir(addTwenty);

When i execute console.dir on addTen and addTwenty you can see the different closures created for two variables for the same function.




At the end what is closure in javascript?
"whenever you declare a function inside another function, the inside function(s) is/are recreated again each time the outside function is called" "Closures are functions that refer to independent (free) variables. In other words, the function defined in the closure 'remembers' the environment in which it was created."

Functions preserves outer scope variable are closures.

9 comments:

genga g said...
This comment has been removed by the author.
saimouni said...

From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
online Python certification course
python training in OMR
python training course in chennai

Unknown said...

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
Devops training in marathahalli
Devops training in rajajinagar

Unknown said...

Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.


devops online training

aws online training

data science with python online training

data science online training

rpa online training

Keerthana said...

The Blog information's are Interesting To Read and Thanks For Sharing Your Valuable Comments

python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

Revathi said...

OpenStack is a free and open-source software & all the industries used for the concept. once learn the this language to get good future in your life..keep sharing!!!


Android Training in Chennai

Android Online Training in Chennai

Android Training in Bangalore

Android Training in Hyderabad

Android Training in Coimbatore

Android Training

Android Online Training

deiva said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
data science training in chennai

data science training in omr

android training in chennai

android training in omr

devops training in chennai

devops training in omr

artificial intelligence training in chennai

artificial intelligence training in omr


jeni said...

Wow, amazing weblog format! How lengthy have you been running a blog for? you make running a blog look easy. The total glance of your website is wonderful, let alone the content!
hardware and networking training in chennai

hardware and networking training in velachery

xamarin training in chennai

xamarin training in velachery

ios training in chennai

ios training in velachery

iot training in chennai

iot training in velachery

kenwood said...

When we talk about Salesforce, it is rated as #1 CRM tool. The service is designed for global availability and redundancy. In other words, a SaaS-based company in its real sense. Salesforce training in Hyderabad