How does the engine resolve a variable inside a function?
It looks in the function's own variable environment first. If not found, it follows the outer environment reference up the scope chain until it finds the variable or reaches the global scope.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How JavaScript Creates Execution Context for Functions
A new function execution context with a variable environment, an outer environment reference (for the scope chain), and a value for this. It is pushed onto the call stack.
A pointer to the parent function's environment. The engine follows these references up the chain to resolve variables not found locally. This is the scope chain.
When the function returns. Its call stack frame is popped and local variables are gone, unless a closure keeps a reference to the variable environment.
Still have questions?
Browse all our FAQs or reach out to our support team
