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
Variables: var, let, const0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
2.1Variables: var, let, const2.2Data Types2.3Comments and Best Practices
HomeTutorialsJavaScript for BeginnersVariables: var, let, const
Data Types

Variables: var, let, const

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

A variable is a named storage container in memory used to hold data values that your program can read, manipulate, and update. In modern JavaScript (ES6+), choosing the correct variable declaration keyword (const, let, or var) is fundamental to writing predictable, bug-free code.


1. Evolution of Variable Declarations

11995 - 2015: Only `var` existed.
2 Features: Function-scoped, hoisted with undefined, allows re-declaration.
3 Flaw: Accidental variable leaks, scoping bugs in loops.
4 |
5 v
62015 (ES6): Introduced `let` and `const`.
7 Features: Block-scoped `{ ... }`, temporal dead zone, no re-declaration.
8 Standard: `const` by default; `let` when reassignment is needed. Never use `var`!

2. The Big Three: var vs let vs const

Featurevar (Legacy)let (Modern Mutable)const (Modern Constant)
ScopeFunction ScopeBlock Scope { }Block Scope { }
Reassignable?YesYesNo (TypeError)
Re-declarable in same scope?Yes (Accidental overwrites!)No (SyntaxError)No (SyntaxError)
Must initialize on declaration?NoNoYes (Must give value!)
Hoisting BehaviorHoisted with undefinedHoisted into Temporal Dead Zone (TDZ)Hoisted into Temporal Dead Zone (TDZ)

3. Scope Mechanics: Function Scope vs. Block Scope

A block in JavaScript is any code enclosed within curly braces { ... } (such as if conditions, for loops, or standalone blocks).

The Danger of var Leaking:

1if (true) {
2 var role = "Admin";
3}
4console.log(role); // "Admin" - Leaked outside the block! var ignores block boundaries!

The Safety of let and const:

1if (true) {
2 let user = "Sumit";
3 const age = 25;
4}
5console.log(user); // ReferenceError: user is not defined!
6console.log(age); // ReferenceError: age is not defined!

4. Reassignment: let vs. const

Use let when values must change:

1let score = 0;
2score = 10; // Valid reassignment!
3score += 5; // score is now 15

Use const for values that should NOT be reassigned:

1const API_URL = "https://api.mskinstitute.com/v1";
2API_URL = "https://other.com"; // TypeError: Assignment to constant variable!

Critical Nuance: const with Objects and Arrays

const prevents reassigning the variable identifier binding, but does not make the internal contents of an object or array immutable!

1const student = { name: "Sumit", marks: 95 };
2 
3// Mutating object properties is 100% VALID:
4student.marks = 98;
5student.city = "Delhi";
6console.log(student); // { name: "Sumit", marks: 98, city: "Delhi" }
7 
8// Reassigning the variable itself throws an ERROR:
9student = { name: "Other" }; // TypeError: Assignment to constant variable!

5. Hoisting and the Temporal Dead Zone (TDZ)

Hoisting is JavaScript's default behavior of moving variable and function declarations to the top of their scope before code execution.

  • With var: Variables are hoisted and initialized with undefined:
1 console.log(x); // undefined (No error, but confusing!)
2 var x = 10;
  • With let and const: Variables are hoisted, but remain in the Temporal Dead Zone (TDZ) until the line of declaration is reached:
1 console.log(y); // ReferenceError: Cannot access 'y' before initialization!
2 let y = 20;

6. Industry Golden Rule: 90/10 Rule

Top engineering teams follow the 90/10 Rule:

  1. 1
    Use const for 90% of your variables by default (objects, arrays, functions, constants).
  2. 2
    Use let only when you know the variable will be reassigned (loop counters, toggles, accumulating scores).
  3. 3
    Never use var in modern codebases.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What type of scope do let and const have?
Question 2
What happens if you attempt to reassign a variable declared with const?
Question 3
Can you modify the properties of an object declared with const (e.g. const user = { name: "Alex" }; user.name = "Sam";)?
Question 4
What is the "Temporal Dead Zone" (TDZ) in JavaScript?
Question 5
Why is var avoided in modern JavaScript development?
Next Lesson

Data Types

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

Related Lessons

Previous LessonNext Lesson
Setting up Environment: VSCode, Live ServerData Types

Practice Quiz

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

Mentor-Led Practical Training

Master JavaScript for Beginners 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