Closures are combination of functions and variables bunded together with references to its surrounding state (the lexical environment).
A closure gives you access to an outer function's scope from an inner function.
Simply closures limit the excess of variables and functions. Like variables and function can only be accessible with in the scope. In the following example scope is simpleCalculator() someVar and add method is only accessible in simpleCalculator function.
function simpleCalculator(firstValue,secondValue,opertaion) {
lwt someVar = 2000;
function add(firstValue,secondValue) {
return firstValue + secondValue;
}
if(opertaion == 'add')
return add(firstValue,secondValue)
}
Another simple example of JavaScript closures
function addBulk(firstValue) {
return function (secondValue) {
return firstValue + secondValue;
};
}
const add10bulk = addBulk(5);
add10bulk(15); //20
add10bulk(30); //30