Closure

closure is a combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.

A closure is a function defined inside another function (called the parent function),
and has access to variables that are declared and defined in the parent function scope.
The closure has access to variables in three scopes:
.Variables declared in their own scope
.Variables declared in a parent function scope
.Variables declared in the global namespace

Example1:
var passed = 3;
var addTo = function(){
         var inner = 2;
         return passed + inner
}
console.log(addTo()) //result 5
Example2:
function makeAdder(x) {
     return function(y) {
          return x + y;
    };
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
Example3:
(function(x) {
     return (function(y) {
        console.log(x+y);
    })(2)
})(1);
// output 3