Closures In JavaScript

What is a closure?

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access to the outer function’s variables and  parameters. What the inner function can’t do is call the outer function’s argument object, but it can call the outer function’s parameters directly. Closures are created by adding a function inside another function, Inception! Let’s take a look a simple example.

Closures are a big part of JQuery. Take a look at pretty much any JQuery code and you’ll find closures sprinkled all over the place. Fastest way to start making your own closures is opening up devTools in your browser (if you’re using Chrome the mac shortcut is option-command-i) and using the console.