Variable
What is a Variable?
A variable is a container that stores data.
Example
let age = 20;
let→ keywordage→ variable name20→ value
var, let, const
1. var (OLD WAY – avoid in modern JS)
var name = "John";
❌ Problems:
- Function scoped (not block scoped)
- Can be re-declared
- Causes bugs
var x = 10; var x = 20; // ✅ allowed (but dangerous)
2. let (MODERN – use this )
let age = 25;
✅ Features:
- Block scoped
- Can be updated
- Cannot be re-declared
let x = 10; x = 20; // ✅ allowed let x = 30; // ❌ error
3. const (CONSTANT – most used )
const pi = 3.14;
✅ Features:
- Block scoped
- Cannot be updated
- Cannot be re-declared
const x = 10; x = 20; // ❌ error
When to use what?
const→ default choice ✅let→ if value changesvar→ ❌ avoid