Skip to main content

πŸ“„ 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​

Behaviorvarletconst
ScopeFunction or globalBlockBlock
InitializationOptionalOptionalRequired
RedeclarationAllowedNot allowedNot allowed
ReassignmentAllowedAllowedNot allowed
Access before declarationReturns undefinedThrows ReferenceErrorThrows 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​

  1. Prefer const: For variables that don't need to be reassigned, use const to improve code readability and maintainability.
  2. Use let secondarily: When reassignment is needed, use let.
  3. 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.
  4. Consider browser compatibility: If support for older browsers is needed, tools like Babel can be used to transpile let and const to var.