36
loading...
This website collects cookies to deliver better user experience
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)
Mozilla
function adder(a) {
return function(b) {
return a + b;
};
}
let add5 = adder(5);
let add10 = adder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
function multiply(y){
let x = Math.pow(10,10);
return x* y;
}
multiply(25); //250000000000
multiply(45); //450000000000
function multiply(){
let x = Math.pow(10,10);
return function(y){
return x* y;
}
}
let multiplier= multiply();
multiplier(25); //250000000000
multiplier(45); //450000000000