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.

21 comments

for ict 99 said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. ES6 Training in Chennai

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

I have been meaning to write something like this on my website and you have given me an idea. Cheers.
Data Science course in rajaji nagar | Data Science with Python course in chenni
Data Science course in electronic city | Data Science course in USA
Data science course in pune | Data science course in kalyan nagar

afiah b said...

Just stumbled across your blog and was instantly amazed with all the useful information that is on it. Great post, just what i was looking for and i am looking forward to reading your other posts soon!
java training in chennai

java training in marathahalli | java training in btm layout

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

jeeva 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.

rpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar

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

dwarakesh said...


Does your blog have a contact page? I’m having problems locating it but, I’d like to shoot you an email. I’ve got some recommendations for your blog you might be interested in hearing.


Amazon Web Services Training in Pune | Best AWS Training in Pune


AWS Training in Chennai | Best Amazon Web Services Training in Chennai


Amazon Web Services Training in Chennai |Best AWS Training in Chennai


Amazon Web Services Online Training | Online Amazon Web Services Certification Course Training

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

varsha said...

Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery


Shanejo said...

It's very useful blog post with inforamtive and insightful content and i had good experience with this information.I have gone through CRS Info Solutions Home which really nice. Learn more details About Us of CRS info solutions. Here you can see the Courses CRS Info Solutions full list. Find Student Registration page and register now. Go through Blog post of crs info solutions. I just read these Reviews of crs really great. You can now Contact Us of crs info solutions. You enroll for Pega Training at crs info solutions.

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

Aadhya said...

Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journal and I normally visit this blog. Examine my administrations.
Go through these Salesforce Lightning Features course. Found this Salesforce CRM Using Apex And Visualforce Training worth joining. Enroll for SalesForce CRM Integration Training Program and practice well. 

Austin said...

You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. Salesforce Training India   

Vennala said...

I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.  
This is really the best Top 20 Salesforce CRM Admin Development Interview Questions highly helpful. I have found these Scenario based Salesforce developers interview questions and answers very helpful to attempt job interviews. Wow, i got this scenario based Salesforce interview questions highly helpful.

Dutta said...

Get real time project based and job oriented Salesforce training India course materials for Salesforce Certification with securing a practice org, database terminology, admin and user interface navigation and custom fields creation, reports & analytics, security, customization, automation and web to lead forms.

Nick569 said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
CRS Info Solutions Salesforce training for beginners 

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

Powered by Blogger.