This line
const myMan = greeting("man", "Bob","girlfiend");
Produces a function that remembers the first parameter (‘man’) because the function greeting only has one parameter; it will ignore the other two.
The produced function, now called ‘myMan’ now receives 2 parameters because that’s how it’s defined. If you call it with 0 arguments myMan()
, the defined parameters will be forced to be filled with undefined
hence the result undefined my man, how are you? How's the undefined?
No matter how many times you call it, it will produce the same output since there’s no side-effects (other than the console.log ofc) or variable mutation. Doesn’t matter if you call it once with the two expected parameters, if you call it again with 0, it will produce what you expect: undefineds. The only paramter being closed over is greet
which is specified in the outer function greeting
the others are still expecting a value.
Responding to a comment:
//Also this function executes itself when declared inside a constant. Why?
That’s because if you assign a function call to a variable/constant, it needs to be executed/called in order to fetch the returning value from the call. There is a console.log statement so it gets executed and there’s no return statement so the interpreter will return undefined
for you.