let, const, and var
Choose block-scoped declarations and understand hoisting and reassignment.
Lesson content
🎯 Goal By the end of this lesson, you'll understand: What are let , const , and var ? When should you use each? What's the difference between them? What are Variables? A variable is a container that stores data. Example: let name = "Ajay" ; Here: let → Keyword name → Variable "Ajay" → Value 1. var var was the original way to create variables before ES6. var name = "Ajay" ; console . log( name ); Output Ajay You can redeclare it var name = "Ajay" ; var name = "Rahul" ; console . log( name ); Output Rahul You can update it var city = "Delhi" ; city = "Mumbai" ; console . log( city ); Output Mumbai ⚠️ Problem: var can cause unexpected bugs because it is function-scoped and allows redeclaration. 2. let let was introduced in ES6 (2015) . Use it when the value may change . let age = 25 ; age = 26 ; console . log( age ); Output 26 ❌ Cannot redeclare let age = 25 ; let age = 30 ; Output SyntaxError ✅ Can update let score = 80 ; score = 90 ; console . log( score ); Output 90 3. const Use const when the value should never change . const PI = 3.14 ; console . log( PI ); Output 3.14 ❌ Cannot update const PI = 3.14 ; PI = 3.14159 ; Output TypeError ❌ Cannot redeclare const PI = 3.14 ; const PI = 3.14159 ; Output SyntaxError Easy Example Student Name (Never Changes) const studentName = "Ajay" ; Student Marks (Can Change) let marks = 80 ; marks = 95 ; Old Way (Avoid) var marks = 80 ; Scope Difference var (Function Scope) if ( true ) { var message = "Hello" ; } console . log( message ); Output Hello The variable is still available outside the if block. let (Block Scope) if ( true ) { let message = "Hello" ; } console . log( message ); Output ReferenceError message exists only inside the {} block. const (Block Scope) if ( true ) { const message = "Hello" ; } console . log( message ); Output ReferenceError Objects with const Many beginners think const makes everything inside an object immutable. It doesn't. const user = { name: "Ajay" }; user . name = "Rahul" ; console . log( user ); Output { name : "Rahul" } ✅ You can change the object's properties. But you cannot reassign the variable itself. user = {}; Output TypeError Comparison Table Feature var let const Redeclare ✅ Yes ❌ No ❌ No Reassign ✅ Yes ✅ Yes ❌ No Scope Function Block Block Hoisted ✅ Yes ✅ Yes* ✅ Yes* Recommended ❌ No ✅ Yes ✅ Best for fixed values * let and const are hoisted but cannot be used before their declaration because of the Temporal Dead Zone (TDZ) . Which One Should You Use? ✅ Use const by default. ✅ Use let if the value will change. ❌ Avoid var in modern JavaScript unless you're working with older code. Interview Question What is the output? let a = 10 ; a = 20 ; const b = 30 ; console . log( a ); console . log( b ); Output 20 30 Key Points var is the old way of declaring variables and should generally be avoided. let is used for variables whose values can change. const is used for variables that should not be reassigned. let and const are block-scoped , while var is function-scoped . Easy Rule to Remember 🔒 const = Fixed value 🔄 let = Value can change ⚠️ var = Old way (avoid in modern JavaScript)