Javascript Closures (ES6 EcmaScript2015)

Closure: A closure is the combination of a function and the lexical environment within which that function was declared.

In simple words closure are functions within function or functions inside another function

All the variables or properties of parent function are visible in child functions: say in below example name variable of parent function myFun() is visible inside child function i.e inside displayName()

Closure Basic program:

Example1:

function myFun() {
var name = “ankit”;
function displayName() {
console.log(name);
}
displayName();
}

Example2:

// Closure same example with different approach

Example3:

function makeAdder(x) {
return function (y) {
return x + y;
}
}

Conclusion

This is it , a bit smaller module but assume will have given a basic understanding about the concept.

For more follow:

Thanks for your time, Have a great Time Cheers..!!