What are closures in javaScript and how and where to use them.
So 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.”
Original source
30 responses to “Javascript Closure tutorial ( Closures Explained )”
Amazing!
best explanation i've seen
Great explanation! This is the first video where I understood closure, Thanks
hello
your videos are great
Closures are nothing but FUNCTIONS WITH PRESERVED DATA"
can we say that as a replica of Static variable in java
You should edit/remove this video. Very first example is not at all closure.
The video was helpful, but this article was even more helpful: https://medium.com/written-in-code/practical-uses-for-closures-c65640ae7304
so this sounds more or less like what scope is? can you please point out the key difference
var passed = 3;
var addTo = function(){
var inner=2;
return passed+inner;
};
console.dir(addTo);
var passed = 4;
console.dir(addTo);
by this code, both the closure have the same value but when we doing console.log answer is different.
both the closure showing 4 in chrome.
but at the time of result, it's showing 5,6 why??
what a boss
Its just awesome
A closure is an inner function that has access to the outer function's variables. Obviously the inner function has access to its own function scope ('local' variables defined between its braces), and it has access to the outer function's variables, which are Closures, and it has access to the global variables.
I love this guy
Thanks for the great video and tutorial. This is how I like to think of
closures. Please feel free to correct me if my understanding of some
concepts aren't correct as I'm just learning about closures myself.
/*
The closure of a function is the function(s) its enclosed in and the
inner function(s) has access to the variables defined in those closures
even after those functions are no longer on the execution stack. Paste
and run the below directly in Google Chrome console (not Fiddle) and drill down into the
scopes to check the closures out. In the below example the inner
function is enclosed in speak function hence it's closure is the
function speak. If speak itself was within another outer function then
inner function has TWO closures the speak function as well as the outer
one enclosing speak.
*/
function speak(greeting)
{
var inner = function (name)
{
console.log(greeting + " " + name);
}
return inner
}
var theGreeting = speak("Hello");
console.log(theGreeting("Adam"));
console.dir(theGreeting);
the new word are used with constructors, it just distracts my attention here
"Closures are functions which use variables outside of the function", I don't know if i can sum it up this way
A closure is when a function uses variables from outside it's local scope. Am I understanding this correctly?
nice tutorial, if "Closures are nothing but FUNCTIONS WITH PRESERVED DATA" Do we need closures in typescript?
Finally understood JS Closure.
addThree is not a variable ,it;s a object sir.
HEllo sir what 9:20
"new" do here please reply
Thank you for excellent video but I have some doubt here and please correct me if I am wrong.
I ran your code in chrome 57.* (latest) version. I used jsfiddler and I am able to see 'passed' var is available under closure prop but it is also showing this closure is part of window.onload function. In that case, its inner function and doesn't agree with your first example.
If I write same logic in console window then 'passed' is showing under 'global' prop. Please explain this.
jsfiddler code
https://jsfiddle.net/rajkishor09/wkv7q1nd/
i cry
I'm new to JavaScript. Having a little confusion. which function is preserving Data?
thank you, for uploading this content.It's unique.
Great video, thanks for making this.
Nice video..
You are amazing. This explanation was the best i've seen.
Well explained!!
Chrome shows different result vs http://jsfiddle.net/jspatel/k444t8vk/ when we execute the function inside the Chrome terminal. Chrome shows [[Scopes]]:Scopes[1] and JSfiddle shows [[Scopes]]:Scopes[2]. Why ?
When I create my own HTML page it doesn't say closure in Chrome. But when I use JSFiddle it does. Same function as above. What's going on?
Thanks, really awesome example! Love the way you word it "Closure are nothing but Functions with Preserved Data"!!!