Closures in JavaScript
What is a closure? A closure is a feature in JavaScript where an inner function has access to the outer (enclosing) function’s variables — a 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 it has access to the global variables To the uninitiated, this definition might seem like just a whole lot of jargon! But what really is a closure? A Simple closure Let’s look at a simple closure example in JavaScript: function outer() { var b = 10; function inner() { var a = 20; console.log(a+b); } return inner; } Here we have two functions: an outer function outer which has a variable b , and returns the inner function an inner function inner which has its variable called a , and accesses an outer variable b , within its function body The scope of variable b ...