JavaScript let, const, and var
2 April 2025 | Category: JavaScript
JavaScript provides three ways to declare variables:
var
(Old, function-scoped, avoid using)let
(Block-scoped, preferred for variables that change)const
(Block-scoped, cannot be reassigned, best for constants)
1️⃣ var
– The Old Way (Avoid Using)
var
was the original way to declare variables in JavaScript before let
and const
were introduced in ES6 (2015).
🔹 Features of var
✅ Can be redeclared
✅ Can be updated (reassigned)
❌ Function-scoped (not block-scoped)
❌ Hoisted with undefined
🔹 Example:
var x = 10;
console.log(x); // Output: 10
var x = 20; // ✅ Allowed (Redeclaration)
console.log(x); // Output: 20
x = 30; // ✅ Allowed (Reassignment)
console.log(x); // Output: 30
🔹 Function Scope Issue:
function test() {
var a = 100;
console.log(a); // ✅ Output: 100
}
console.log(a); // ❌ Error: a is not defined (Only exists inside function)
🔹 Hoisting with var
Variables declared with var
are hoisted, meaning they are moved to the top but remain undefined
until assigned a value.
console.log(y); // Output: undefined
var y = 5;
console.log(y); // Output: 5
2️⃣ let
– The Modern Way (Recommended for Variables)
Introduced in ES6, let
fixes the problems of var
by being block-scoped and not hoisted like var
.
🔹 Features of let
✅ Cannot be redeclared in the same scope
✅ Can be updated (reassigned)
✅ Block-scoped (Exists only inside {}
)
❌ Not hoisted like var
🔹 Example:
let city = "New York";
console.log(city); // Output: New York
// let city = "London"; ❌ Error: Cannot redeclare
city = "London"; // ✅ Allowed (Reassignment)
console.log(city); // Output: London
🔹 Block Scope Example:
if (true) {
let score = 100;
console.log(score); // ✅ Output: 100
}
console.log(score); // ❌ Error: score is not defined (Only exists inside block)
🔹 Hoisting with let
Unlike var
, let
is not initialized at the top of the script.
console.log(age); // ❌ Error: Cannot access 'age' before initialization
let age = 25;
3️⃣ const
– Best for Constants (Cannot be Changed)
const
is another ES6 feature that prevents reassignment. Use it for values that should never change (e.g., API keys, fixed numbers).
🔹 Features of const
✅ Cannot be redeclared
✅ Cannot be updated (reassigned)
✅ Block-scoped
✅ More secure (Ensures values stay constant)
🔹 Example:
const PI = 3.14159;
console.log(PI); // Output: 3.14159
// PI = 3.14; ❌ Error: Assignment to constant variable
🔹 Works with Objects and Arrays (but properties can change)
const user = { name: "John", age: 25 };
user.age = 26; // ✅ Allowed (Modifying property)
console.log(user.age); // Output: 26
// user = { name: "Alice" }; ❌ Error: Cannot reassign the entire object
4️⃣ Key Differences Between var
, let
, and const
Feature | var | let | const |
---|---|---|---|
Scope | Function-scoped | Block-scoped | Block-scoped |
Can be updated? | ✅ Yes | ✅ Yes | ❌ No |
Can be redeclared? | ✅ Yes | ❌ No | ❌ No |
Hoisting? | ✅ Hoisted but undefined | ❌ Not hoisted | ❌ Not hoisted |
Best Use Case | ❌ Avoid using | ✅ Use for variables that change | ✅ Use for constants |
5️⃣ Best Practices for Using Variables
✅ Use const
by default.
✅ Use let
only when you need to reassign a variable.
❌ Avoid using var
to prevent unexpected errors.
✅ Use meaningful names (userAge
, totalAmount
).
🚀 Conclusion
✔️ var
is outdated, avoid using it.
✔️ let
is great for variables that change.
✔️ const
is best for constants that don’t change.
✔️ Always follow best practices for better, safer JavaScript code!