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
Block Scope with let & const0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
HomeTutorialsJavaScript for IntermediateBlock Scope with let & const
Template Literals

Block Scope with let & const

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

Block Scope with let and const

In early JavaScript (ES5 and earlier), variable scoping was exclusively determined by functions. With the arrival of ECMAScript 2015 (ES6), block scoping was introduced via let and const. Understanding how block scopes, lexical environments, and the Temporal Dead Zone (TDZ) function under the hood is fundamental to intermediate JavaScript mastery.


1. Scoping Evolution: Function Scope vs. Block Scope

A block in JavaScript is defined as any pair of matching curly braces { ... }:

  • Inside if / else statements
  • Inside for, while, and do...while loops
  • Inside standalone blocks { ... }
1// Function Scope (Legacy var):
2function testVar() {
3 if (true) {
4 var greeting = "Hello from var";
5 }
6 console.log(greeting); // "Hello from var" - Leaked outside the if block!
7}
8 
9// Block Scope (Modern let & const):
10function testLetConst() {
11 if (true) {
12 let message = "Hello from let";
13 const pi = 3.14159;
14 }
15 console.log(message); // ReferenceError: message is not defined!
16 console.log(pi); // ReferenceError: pi is not defined!
17}

2. The Classic Closure in Loops Problem

Consider this classic frontend interview challenge:

1// ❌ BROKEN WITH VAR:
2for (var i = 0; i < 3; i++) {
3 setTimeout(() => {
4 console.log(i);
5 }, 100);
6}
7// Outputs: 3, 3, 3! (Because var i is shared across the entire function scope!)

Why let Solves This Elegantly:

In JavaScript engines, let inside a for loop header creates a brand new lexical binding for every single iteration:

1// ✅ FIXED WITH LET:
2for (let i = 0; i < 3; i++) {
3 setTimeout(() => {
4 console.log(i);
5 }, 100);
6}
7// Outputs: 0, 1, 2! (Each timeout closure captures its own private block-scoped `i`!)

3. The Temporal Dead Zone (TDZ) Deep Dive

Many developers mistakenly believe that let and const are not hoisted. They ARE hoisted, but unlike var (which is initialized with undefined), let and const remain uninitialized in the Temporal Dead Zone (TDZ):

1// Proof of TDZ hoisting:
2let value = "outer";
3 
4function demonstrateTDZ() {
5 // If 'let value' was not hoisted, this would log "outer".
6 // Because it IS hoisted to this block scope, accessing it before initialization throws!
7 console.log(value); // ReferenceError: Cannot access 'value' before initialization!
8 let value = "inner";
9}
10 
11demonstrateTDZ();

4. Shadowing Variables

Variable shadowing occurs when a variable declared within an inner block scope has the same identifier as a variable in an outer scope:

1const userRole = "viewer"; // Outer scope
2 
3function checkAccess() {
4 if (true) {
5 const userRole = "admin"; // Shadows outer userRole inside this block!
6 console.log(`Inner role: ${userRole}`); // "admin"
7 }
8 console.log(`Outer role: ${userRole}`); // "viewer"
9}
10 
11checkAccess();

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What creates a new block scope in modern JavaScript?
Question 2
Why does for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 10); } print 3, 3, 3?
Question 3
What happens when accessing a let variable during its Temporal Dead Zone (TDZ)?
Question 4
Can a let variable be re-declared within the exact same block scope?
Question 5
What is "variable shadowing"?
Next Lesson

Template Literals

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

Related Lessons

Previous LessonNext Lesson
NoneTemplate Literals

Practice Quiz

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

Mentor-Led Practical Training

Master JavaScript for Intermediate 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
Next
PreviousPrevNext