DEV Community

Discussion on: The JavaScript Execution Context, Call-stack & Event Loop

Collapse
 
mhnd3 profile image
Mhnd3

I think i got it somehow, This has to do with closures, when the function was declared, not only the function declaration was defined, but a closure was created that holdes not only the function declaration, but also all the variables that are in the scope at the point of the function declaration, so when the anonymous function executes, even though the scope in which it was declared gone away, it still it has access to the original scope in which it was declared through its closure object.

This explanation from Mozilla would clear the idea i think: developer.mozilla.org/en-US/docs/W...