0

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
pilchard
  • 9,782
  • 5
  • 9
  • 21
  • *"clouser"*? you mean ***closure***? Closures are explained in depth all around the web and here too: [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Roko C. Buljan May 15 '22 at 12:18

0 Answers0