0

I am trying to understand how closure works. Please take a look at below code

if (true) {
    let a = 40;

    function add(b) {
        return () => {
            let c = a + b;
            console.log(c);
        }
    }

    console.dir(add(10));
}

Case1: a variable is in block scope

Case2: b variable is in closure scope why? Not understanding

Output:

enter image description here

Please put some light on Case2? why is it so?

Akki619
  • 2,386
  • 6
  • 26
  • 56
  • Function returned by the `add` function has a closure over the local scope of `add` function and the local scope of function `a` includes the parameter `b` as well. Function `add` has a closure over the `if` block which includes variable `a`. Related: https://stackoverflow.com/questions/69266831/till-what-level-do-closures-keep-their-execution-contexts. In short, closure is just a reference to the environment in which a function is created. Reference to the environment is saved in the internal `[[Environment]]` slot on the function object. – Yousaf Sep 24 '21 at 07:08
  • 2
    They're both closures. Stop thinking that closure is a type of scope. It's not. A closure is an instance of scope. For example in your code above `40` is a number and `a` is 40. You would not consider `a` to be a kind of 40. Instead `a` is an instance of a number that stores the value 40. Similarly a closure is an instance of a scope. Closures stores scopes. – slebetman Sep 24 '21 at 07:10

1 Answers1

0

a is in block scope for the anonymous function returned by calling add and b is in the closure scope for the anonymous function returned by calling add

so a function is going to keep any variables needed in the closure scope even when the outer function has exited and all its local variables are garbage collected except b which is needed for the anonymous function . hence it lives in the closure scope of it

hence you may add more to closure scope like so;

if (true) {
    let a = 40;

    function add(b) {
        let d = 55;
        return () => {
            let c = a + b +d;
            console.log(c);
        }
    }

    console.dir(add(10));
}

now in the closure scope we have b and d ( they both are local variables to function add and are needed by the inner function)

similarly if the variable d was not required, then it won't be in its closure scope like so

if (true) {
let a = 40;

function add(b) {
    let d = 55;
    return () => {
        let c = a + b;
        console.log(c);
    }
}

console.dir(add(10));

}

now in the closure scope we only have b.

so its not the function passed in parameter that is assigned in closure scope. it can be any local variable which is needed. for eg:

if (true) {
    let a = 40;

    function add(b) {
        let d = 55;
        return () => {
            let c = a + d;
            console.log(c);
        }
    }

    console.dir(add(10));
}

now we dont have b in closure even it is a passed parameter. its just d in closure scope.

Reetesh Kumar
  • 452
  • 2
  • 8