I am trying to understand closures in depth. Consider this example from w3schools:
var add = (function outerAdd() {
var counter = 0;
return function innerAdd() {
counter += 1;
return counter
}
})();
console.log(add());
console.log(add());
console.log(add());
// the counter is now 3
The page says that 'Local variables have short lives. They are created when the function is invoked, and deleted when the function is finished.' This means that counter is deleted after the outer self invoking function outerAdd runs.
But the inner returned function innerAdd is still able to access counter because of the scope chain that was formed when innerAdd was defined (or executed?). So now the question is, does the scope chain copy the variable values when a new execution context is created? Because if the scope chain simply maintained a pointer to counter, it should throw an error as counter got deleted after outerAdd function finished running.
Also, what if counter was an object? How will the 'copying' behaviour work then? As objects are copied by reference in JavaScript.
EDIT: I appreciate all the answers here. But what I am really trying to understand is the inner working of closures, as explained by execution contexts and scope chains. So I am looking for an explanation which is something on those lines. Other Stackoverflow answers don't really explain how this happens internally?
Like I don't understand when exactly was counter referred by the scope chain of innerAdd? If it was during execution of innerAdd (i.e. when an execution context was formed for innerAdd), then why was counter not garbage collected at that time? As once outerAdd finished execution, there is nothing referencing counter.