MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Scope Chain & Lexical Environment0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
HomeTutorialsJavaScript for AdvancedScope Chain & Lexical Environment
Class Syntax & Constructor Methods

Scope Chain & Lexical Environment

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

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.


1. Lexical Scoping Explained

JavaScript uses Lexical Scoping (also called Static Scoping). This means:

The scope of an identifier is determined by its physical location in the source code at author time, not where the function is called!
1const user = 'Global Alice';
2 
3function displayUser() {
4 console.log(user);
5}
6 
7function outer() {
8 const user = 'Local Bob';
9 displayUser(); // What will this print?
10}
11 
12outer(); // Prints: "Global Alice" (NOT "Local Bob"!)

Why? Because displayUser was lexically declared in the global scope. It resolves identifiers based on where it was born, not where it was invoked.


2. Structure of a Lexical Environment

Under the hood, every execution context has an associated Lexical Environment consisting of two components:

  1. 1
    Environment Record: The dictionary storing local variables, function declarations, and arguments.
  2. 2
    Reference to the Outer Lexical Environment: A pointer to the parent environment in which the function was physically created.
1┌───────────────────────────────────────────────────────────┐
2│ Global Lexical Environment │
3│ • Environment Record: { user: 'Global Alice' } │
4│ • Outer Reference: null │
5└─────────────────────────────▲─────────────────────────────┘
6 │
7┌─────────────────────────────┴─────────────────────────────┐
8│ displayUser() Lexical Environment │
9│ • Environment Record: { } │
10│ • Outer Reference: ──────────► Points to Global! │
11└───────────────────────────────────────────────────────────┘

3. The Scope Chain Lookup Mechanism

When a variable is accessed inside a function:

  1. 1
    The engine checks the current function's local Environment Record.
  2. 2
    If not found, it follows the outer reference to the parent Lexical Environment.
  3. 3
    It repeats this traversal up the chain until finding the variable.
  4. 4
    If it reaches the Global Environment (outer === null) and still cannot find the variable, it throws a ReferenceError.
1const globalA = 'A';
2 
3function levelOne() {
4 const levelOneB = 'B';
5 
6 function levelTwo() {
7 const levelTwoC = 'C';
8 // Can access C (local), B (parent), and A (grandparent)!
9 console.log(levelTwoC, levelOneB, globalA);
10 }
11 
12 levelTwo();
13}
14 
15levelOne(); // Output: "C B A"

4. Block Scope vs Function Scope

  • 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.
1if (true) {
2 var leakedVar = 'I escaped!';
3 let trappedLet = 'I am trapped inside!';
4}
5 
6console.log(leakedVar); // 'I escaped!'
7// console.log(trappedLet); // ReferenceError: trappedLet is not defined

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does "Lexical Scoping" mean in JavaScript?
Question 2
What are the two internal components of a Lexical Environment in the ECMAScript specification?
Question 3
What happens when JavaScript looks for a variable that does not exist in any lexical environment up to the global scope?
Question 4
Which declaration keyword is NOT confined by if or for blocks?
Question 5
In nested functions, can an outer function access variables declared inside an inner child function?
Next Lesson

Class Syntax & Constructor Methods

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Execution Context & Call Stack Deep DiveClass Syntax & Constructor Methods

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext