What's the difference between var, let, and const?
var is function-scoped, can be redeclared, and hoisted with undefined. let is block-scoped, cannot be redeclared in the same scope, and hoisted but not initialized (temporal dead zone). const is block-scoped, must be initialized at declaration, cannot be reassigned (but objects/arrays can be mutated), and also has temporal dead zone. Modern code prefers const by default, let when reassignment is needed, and avoids var.
1// var - function scoped, can be redeclared2var x = 10;3var x = 20; // OK4if (true) {5 var x = 30; // Same variable6}7console.log(x); // 3089// let - block scoped, cannot be redeclared10let y = 10;11// let y = 20; // Error: already declared12if (true) {13 let y = 30; // Different variable14 console.log(y); // 3015}16console.log(y); // 101718// const - block scoped, cannot be reassigned19const z = 10;20// z = 20; // Error: assignment to constant21const obj = { name: "John" };22obj.name = "Jane"; // OK - object mutation allowed23// obj = {}; // Error: reassignment not allowed