I am curious how the closure in Javascript removed.
I know the definition of closure. It is a variable which is referred by child function inside of a parent function that lifecycle is over.
Here is the example below:
let inner;
function outerFunction() {
let outerValue = 'Mike'
function innerFunction() {
let message = `${outerValue} is a developer`
console.log(message) // Mike is a developer
}
inner = innerFunction;
return
}
outerFunction()
inner()
As you can see, when outerFunction is called, eventually it is removed from callstack due to return statement.
After outerFunction, inside which is the variable assigned innerFunction is called.
innerFunction shows the message 'Mike is a developer'. However it does not return.
In this case, when does innerFunction is removed from callstack?
The function does not return and hold a variable from outerFunction. If this remains in memory then it must be a huge waste.
Or is innerFunction also removed when outerFunction returns?