π Please explain the differences between var, let, and const
Overviewβ
JavaScript has three keywords for declaring variables: var
, let
, and const
. While they are all used to declare variables, they differ in terms of scope, initialization, redeclaration, reassignment, and accessibility timing.
Main Differencesβ
Behavior | var | let | const |
---|---|---|---|
Scope | Function or global | Block | Block |
Initialization | Optional | Optional | Required |
Redeclaration | Allowed | Not allowed | Not allowed |
Reassignment | Allowed | Allowed | Not allowed |
Access before declaration | Returns undefined | Throws ReferenceError | Throws ReferenceError |
Detailed Explanationβ
Scopeβ
var
has function or global scope, while let
and const
have block scope (including functions, if-else blocks, or for loops).
function scopeExample() {
var varVariable = 'var';
let letVariable = 'let';
const constVariable = 'const';
console.log(varVariable); // 'var'
console.log(letVariable); // 'let'
console.log(constVariable); // 'const'
}
scopeExample();
console.log(varVariable); // ReferenceError: varVariable is not defined
console.log(letVariable); // ReferenceError: letVariable is not defined
console.log(constVariable); // ReferenceError: constVariable is not defined
if (true) {
var varInBlock = 'var in block';
let letInBlock = 'let in block';
const constInBlock = 'const in block';
}
console.log(varInBlock); // 'var in block'
console.log(letInBlock); // ReferenceError: letInBlock is not defined
console.log(constInBlock); // ReferenceError: constInBlock is not defined
Initializationβ
var
and let
can be declared without initialization, while const
must be initialized at declaration.
var varVariable; // Valid
let letVariable; // Valid
const constVariable; // SyntaxError: Missing initializer in const declaration
Redeclarationβ
Within the same scope, var
allows redeclaration of the same variable, while let
and const
do not.
var x = 1;
var x = 2; // Valid, x is now 2
let y = 1;
let y = 2; // SyntaxError: Identifier 'y' has already been declared
const z = 1;
const z = 2; // SyntaxError: Identifier 'z' has already been declared
Reassignmentβ
var
and let
can be reassigned, while const
cannot be reassigned.
var x = 1;
x = 2; // Valid
let y = 1;
y = 2; // Valid
const z = 1;
z = 2; // TypeError: Assignment to a constant variable
Note: Although variables declared with const
cannot be reassigned, if it's an object or array, its contents can still be modified.
const obj = { key: 'value' };
obj.key = 'new value'; // Valid
console.log(obj); // { key: 'new value' }
const arr = [1, 2, 3];
arr.push(4); // Valid
console.log(arr); // [1, 2, 3, 4]
Access before declaration (Temporal Dead Zone)β
Variables declared with var are hoisted and automatically initialized to undefined, while variables declared with let and const are also hoisted but not initialized, throwing a ReferenceError if accessed before declaration.
console.log(x); // undefined
var x = 5;
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 5;
console.log(z); // ReferenceError: Cannot access 'z' before initialization
const z = 5;
Best Practicesβ
- Prefer
const
: For variables that don't need to be reassigned, useconst
to improve code readability and maintainability. - Use
let
secondarily: When reassignment is needed, uselet
. - Avoid
var
: Due to var's scope and hoisting behavior that can lead to unexpected issues, it's recommended to avoid it in modern JavaScript development. - Consider browser compatibility: If support for older browsers is needed, tools like Babel can be used to transpile
let
andconst
tovar
.