Variables are used to store, access, and manipulate data in JavaScript. They act as references to values stored in memory, allowing programs to work dynamically.
🔹 1. var vs let vs const
Why const Is the Modern Default
JavaScript provides three ways to declare variables, but modern JavaScript prefers const and let.
🧪 Comparison Table
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function | Block | Block |
| Re-declaration | ✅ Allowed | ❌ Not allowed | ❌ Not allowed |
| Re-assignment | ✅ Allowed | ✅ Allowed | ❌ Not allowed |
| Hoisting | Yes (undefined) | Yes (TDZ) | Yes (TDZ) |
| Modern Usage | ❌ Avoid | ✅ Yes | ✅ Default |
✅ Why const Is the Default
- Prevents accidental reassignment
- Makes code safer and more predictable
- Signals intent: “This reference should not change”
const pi = 3.14; // Good let counter = 0; // When value must change
📌 Rule of Thumb:
Use
constby default → switch toletonly if reassignment is required → avoidvar.
🔹 2. Declaration vs Assignment vs Initialization
These terms describe different moments in a variable’s lifecycle.
📘 Declaration
Reserving a variable name in memory.
let age;
📘 Assignment
Giving a value to an already declared variable.
age = 25;
📘 Initialization
Declaration and assignment together.
let age = 25;
🔄 Visual Flow
Declaration → Assignment → Usage
🔹 3. Metaphor: Buckets vs Tentacles (Memory References)
Understanding variables is easier with metaphors.
🪣 Old Metaphor: “Buckets”
- Variables are containers holding values
- Works well for primitives
let a = 10; let b = a; b = 20;
astays10- Values are copied
📌 Good for:
- Numbers
- Strings
- Booleans
🐙 Better Metaphor: “Tentacles / Labels” (Modern View)
Variables are labels pointing to memory locations, not the value itself.
const user = { name: "Deepak" };
const admin = user;
admin.name = "Admin";
- Both
userandadminpoint to the same object - Changing one affects the other
📌 This explains:
- Reference behavior
- Objects & arrays
- Why
constobjects can still change
🧠 Key Insight
constprotects the reference, not the value.
const arr = [1, 2, 3]; arr.push(4); // ✅ Allowed arr = []; // ❌ Error

Leave a Reply