Lexical Scope and the Scope Chain
JavaScript uses lexical scoping: where a function is written determines which outer variables it can access. When an identifier is referenced, JavaScript searches the current lexical environment and then walks outward through enclosing environments until it finds the binding or reaches the global environment.
This lexical relationship is the foundation for closures.
Scope Lookup
Key Takeaways
- Scope is determined by lexical structure.
- Identifier lookup proceeds through enclosing environments.
- Closures depend on lexical environments remaining accessible.
Knowledge Check
What primarily determines which outer variables a function can access?