Javascript Closures Tutorial – What makes Javascript Weird…and Awesome Pt 3




What is a closure? In this Javascript Tutorial, we’re going to be learning about closures – our 3rd most misunderstood concept of Javascript.

Watch the full playlist:

Hopefully, we’re going to break it down enough to make it simple to understand for newbies and intermediate coders alike.

Javascript closures are really not that complex of an idea.

The textbook definition of a closure is a function that retains state and scope after it executes. Basically put, when the function’s done, a callback might need to access the variables inside later…so Javascript remembers those variables. That remembering of the variables is called a closure.

Original source


32 responses to “Javascript Closures Tutorial – What makes Javascript Weird…and Awesome Pt 3”

  1. Hi, I'm slowly moving over from AS3 to JS. I'm wondering if (in this example) a were an object or an array, would you have to nullify the object or array before it gets picked up by garbage collection? For example, a = ["val1", "val2"]. (then before closure) a = []. AS3 had an odd tendency to keep certain things in memory – is this the same with JS?

  2. wow. i wish jason from codeschool explained it like this. cant believe i still did not know what a closure was. he really explained it in depth though, i wonder why i couldnt comprehend it from him. is this a simple way of explaining it? are there more complicated expressions of closures? whats a good place to read further on closures? thanks man.

  3. I'm amazed by the quality of the content you're putting up here! There's a ton of JS material out there but the fact that this is about the 20th of your videos that I'm watching speaks for itself.
    Just to help fellow learners along, a lot of these JS concepts are super weird when you come from more classical object-oriented languages like Java (as it was the case for me). A really good summary of essential JS concepts with respect to functional programming can be found here: http://jrsinclair.com/articles/2016/gentle-introduction-to-functional-javascript-intro/

  4. Will, Where is the memory that is storing the javascript code and all the closures? Is the browser taking a small memory block out of your computer to store it every time you load the page? Just wanted to know.

  5. Sorry if I didn't catch it, but just to confirm, the thing that is called the closure here is the variable 'a'. Is that correct? I've seen the word closure used as an adjective instead of a noun before so I'm a bit confused

  6. It would be awesome if you can explain how to use IIFE with closures. Lets say if an loop you want to remember and return each looped value instead of the last value. Until this day, I am still confusing about it.
    Btw, I just want to say your tutorials are the best so far. Much better than many large website's vague tutorial

  7. I was almost wondering where are the rest two videos, then I realize you made this one only a week ago. Thanks for making this series. You can explain things so clearly and so effectively. It's incredibly hard to find a good video like this!

    I think people might want a release schedule for your tutorials 🙂

  8. ALready waiting for the rest. And I think you need to add another one JS Objects. There are many ways to create an object and how and why you can initialize function as an object with "new" state.

Leave a Reply