A variable in JavaScript is a container for storing data values.Variables allow you to store, update, and reuse data in your programs.
1. Declaring Variables in JavaScript
JavaScript provides three keywords to declare variables:
2. var – Function Scoped (Old Way)
Characteristics:
- Function scoped
- Can be redeclared
- Can be reassigned
- Hoisted (initialized as undefined)
Example:
❌ Not recommended in modern JavaScript
3. let – Block Scoped (Modern)
Characteristics:
- Block scoped { }
- Cannot be redeclared in the same scope
- Can be reassigned
- Hoisted but not initialized (Temporal Dead Zone)
Example:
4. const – Constant Variables
Characteristics:
- Block scoped
- Cannot be redeclared
- Cannot be reassigned
- Must be initialized at declaration
Example:
5. const with Objects and Arrays
You can modify properties, but cannot reassign the variable.
