-4

I am currently trying to understand closures but there is something which I don't seem to get no matter how many videos or forum posts I check.

As an example, here is a simple closure with a parentFunction and a childFunction:

function parentFunction(a, b) {
    function childFunction() {
        return a + b;
    }
    return childFunction;
}

What I don't understand is why is it necessary to assign the parentFunction to a variable and call it if I want the value returned from the childFunction:

let test = parentFunction(1, 2)

console.log(test());

If I called the parent function directly, shouldn't it have the same outcome? For example:

console.log(parentFunction(1, 2));

Isn't it the same as assigning it to the variable first but with an extra step? Is it just because of the syntax being that way and that's it?

Nyxnik
  • 15
  • 4
  • `parentFunction` returns another function. You'd get the same output if you called the returned function: `console.log(parentFunction(1,2)())` – Joachim Jul 12 '22 at 21:36
  • @CertainPerformance That is not what is happening though, at least according to Chrome's console, when logging the variable, it returns the value of the `childFunction` (in this case it returns 3) but if I log the `parentFunction` directly I get the whole `childFunction` statement and not the returned value. Hope this makes sense. – Nyxnik Jul 12 '22 at 21:37
  • in `let test = parentFunction(1, 2)`, test is not a variable, it is a function... – Mister Jojo Jul 12 '22 at 21:44
  • @MisterJojo Maybe I phrased that incorrectly but am I not declaring the variable test and assigning the value (which in this case is a function) to it? – Nyxnik Jul 12 '22 at 21:54
  • 1
    declaring a new javascript element by assigning it a function, in javascript this means declaring a function . have a look to https://stackoverflow.com/questions/58262667/is-this-really-considered-a-javascript-closure/58263202#58263202 – Mister Jojo Jul 12 '22 at 22:51

1 Answers1

1

When you put it into a variable first, you're also invoking it when logging the result.

let test = parentFunction(1, 2)
console.log(test());
//              ^^

Substituting in parentFunction(1, 2) and removing the test variable entirely would be equivalent

console.log(test                ()); // before
console.log(parentFunction(1, 2)()); // after

But your second version lacks the () at the end, which is why it's not the same.

console.log(parentFunction(1, 2)()); // after
//                              ^^ your second version lacks this

parentFunction returns a function. You must call the function to get the resulting number from it, otherwise you have just the function.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • there is a very famous question about closures, I guess there is no need in answering this one – dippas Jul 12 '22 at 21:38
  • @dippas it's nice that he got a specific answer, he maybe just got confused. no link-only answer would help in this case – Joachim Jul 12 '22 at 21:45
  • 1
    @CertainPerformance OMG so that's what was happening... Damn, I didn't even see it haha. Now this makes perfect sense, thank you for the quick response! – Nyxnik Jul 12 '22 at 21:45
  • @dippas As for the post you suggested, thanks but I did take a look at it and since in their examples they also assigned the function to a variable, It didn't help with this issue I had. Cheers! – Nyxnik Jul 12 '22 at 21:46