Explaining Javascript Closures to Granny
Where to start. Let’s imagine you meet a new person, and he or she tells you his or her background, such as their name, age, and where they’re from. Let’s consider that the telling of that person’s background as being our function. As you try to remember all the information they’ve told you, the place where you store that information is your very short term memory, and in Javascript terms, your variable. Chances are, after they’ve finished talking, you’ve forgotten everything. That is similar to what happens to variables after functions are completed, they are lost.
Get Michael Im’s stories in your inbox
Join Medium for free to get updates from this writer.
However, imagine that it turns out that the person you are talking to is from a town called, Franklin, and you associate that location with the person who was president of the United States during your childhood, FDR. That process of association helps you remember the name of the town, and that process is similar to a function within your original function (the telling of the person’s background). Because you have made that association, you keep that information alive. That’s kind of like a closure.
One last thing about closures…You have the ability to recall the town of Franklin, but the person you’re talking to doesn’t have access to the fact that FDR was president during your childhood. Your information is protected, similar to a closure.





