I'm learning closure concept in js. can anyone tell me how the previous value is stored in function? and when i call plusOne(3); the valus stores in y variable. how?
function makeAdder(x) {
console.log(`this is makeAdder function, ${x}`);
function add(y) {
console.log(`this is add function, where x = ${x}`);
console.log(`this is add function, where y = ${y}`);
// console.log(`this is add function, ${x+y}`);
return y + x;
};
console.log(add);
return add;
}
var plusOne = makeAdder(2);
plusOne(3);
// plusOne( 3 ); // 4 <-- 1 + 3
// plusOne( 41 ); // 42 <-- 1 + 41
// plusTen( 13 ); // 23 <-- 10 + 13