Scope determines the visibility and accessibility of variables in JavaScript. Understanding how the JavaScript engine resolves variable references across nested functions requires mastering the Lexical Environment and the Scope Chain.
JavaScript uses Lexical Scoping (also called Static Scoping). This means:
Why? Because displayUser was lexically declared in the global scope. It resolves identifiers based on where it was born, not where it was invoked.
Under the hood, every execution context has an associated Lexical Environment consisting of two components:
When a variable is accessed inside a function:
outer reference to the parent Lexical Environment.outer === null) and still cannot find the variable, it throws a ReferenceError.var is Function Scoped: Ignores curly brace blocks (if, for, while). It leaks outside blocks!let and const are Block Scoped: Create a new lexical block environment for every { ... } pair.Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Execution Context & Call Stack Deep Dive | Class Syntax & Constructor Methods |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.