Differences between JavaScript variables created using let, var or const
var is the old way of creating variables, while let and const are the modern, safer ways.
let: can be reassigned.const: cannot be reassigned.var: works differently and often leads to bugs.
Differences in behavior
Scope
var: function-scoped.let&const: block-scoped.
Hoisting
var: hoisted, initialized asundefined.let&const: hoisted but in the Temporal Dead Zone (TDZ) → error if accessed early.
Re-declaration & Re-assignment
var: can redeclare & reassign.let: can reassign but not redeclare.const: neither redeclare nor reassign (but object/array contents are mutable).
Global binding
varattaches towindowin browsers.let&constdo not.
// Scope
if (true) {
var a = 1; // function-scoped
let b = 2; // block-scoped
const c = 3; // block-scoped
}
console.log(a); // 1
// console.log(b); // Error
// console.log(c); // Error
// Hoisting
console.log(x); // undefined
var x = 10;
// console.log(y); // ReferenceError (TDZ)
let y = 20;
// Re-declaration & Re-assignment
var m = 1; var m = 2; // allowed
let n = 3; // let n = 4; // Error
const p = 5; // p = 6; // Error
// const with objects
const obj = { name: "John" };
obj.name = "Mike"; // allowed (mutating object property)