var, let & const
Variable Declaration Keywordsโ
var (ES5)โ
- Function-scoped or globally-scoped
- Can be redeclared and updated
- Hoisted with
undefinedvalue
var name = "John";
var name = "Jane"; // Redeclaration allowed
name = "Bob"; // Update allowed
function example() {
var x = 1;
if (true) {
var x = 2; // Same variable
console.log(x); // 2
}
console.log(x); // 2
}
let (ES6)โ
- Block-scoped
- Cannot be redeclared in same scope
- Can be updated
- Hoisted but not initialized (Temporal Dead Zone)
let age = 25;
// let age = 30; // Error: Cannot redeclare
age = 30; // Update allowed
function example() {
let y = 1;
if (true) {
let y = 2; // Different variable (block scope)
console.log(y); // 2
}
console.log(y); // 1
}
const (ES6)โ
- Block-scoped
- Cannot be redeclared or updated
- Must be initialized at declaration
- Hoisted but not initialized (Temporal Dead Zone)
const PI = 3.14159;
// const PI = 3.14; // Error: Cannot redeclare
// PI = 3.14; // Error: Cannot update
// Must initialize
// const value; // Error: Missing initializer
Important Differencesโ
Scope Comparisonโ
function scopeExample() {
var varVariable = "var";
let letVariable = "let";
const constVariable = "const";
if (true) {
var varInBlock = "var in block";
let letInBlock = "let in block";
const constInBlock = "const in block";
}
console.log(varInBlock); // "var in block" - accessible
// console.log(letInBlock); // Error: not defined
// console.log(constInBlock); // Error: not defined
}
Hoisting Behaviorโ
// var hoisting
console.log(hoistedVar); // undefined (not error)
var hoistedVar = "I'm hoisted";
// let/const hoisting (Temporal Dead Zone)
// console.log(hoistedLet); // Error: Cannot access before initialization
let hoistedLet = "I'm in TDZ";
// console.log(hoistedConst); // Error: Cannot access before initialization
const hoistedConst = "I'm in TDZ";
Object/Array with constโ
// const prevents reassignment, not mutation
const arr = [1, 2, 3];
arr.push(4); // Allowed - mutating content
console.log(arr); // [1, 2, 3, 4]
// arr = [5, 6, 7]; // Error: Cannot reassign
const obj = { name: "John" };
obj.name = "Jane"; // Allowed - mutating property
obj.age = 25; // Allowed - adding property
console.log(obj); // { name: "Jane", age: 25 }
// obj = {}; // Error: Cannot reassign
Best Practicesโ
- Use
constby default - Use
letwhen you need to reassign the variable - Avoid
varin modern JavaScript - Always declare variables at the top of their scope
Quick Reference Tableโ
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function/Global | Block | Block |
| Redeclare | โ | โ | โ |
| Update | โ | โ | โ |
| Hoisting | โ (undefined) | โ (TDZ) | โ (TDZ) |
| Initialize | Optional | Optional | Required |