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

var passed = 3;
var addTo = function(){
         var inner = 2;
         return passed + inner
console.log(addTo()) //result 5
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
(function(x) {
     return (function(y) {
// output 3

Advantages of using Closures
As we all know variables that we create inside the function have local scope and are only accessible inside the function not outside the function.
1 -Also variable defined inside the function created when we call function and destroyed which function close. We can define global variables which created when the program starts till the end of the program and are accessible anywhere in the program.
2- If we define the global variable these can be changed anywhere in the program.
Solution :
Data Encapsulation
we can overcome the above problems by using closures.
1. By using a closure we can have private variables that are available even after a function task is finished.
2. With a function closure we can store data in a separate scope, and share it only where necessary.