let and const in JavaScript: A Complete Guide
let and const replaced var in modern JS. Here is how they work, how they differ from var, and when to use each.
let and const in JavaScript: A Complete Guide
let and const were introduced in ES6 to fix the problems var caused. They are block-scoped, not hoisted with undefined, and stricter about re-declaration.
let: Block-Scoped, Reassignable
let x = 5; x = 10; // OK, let allows reassignment `` `let` is for variables that will change. ### `const`: Block-Scoped, Not Reassignable ```js const y = 5; y = 10; // TypeError: Assignment to constant variable `` `const` is for variables that will not be reassigned. But `const` does not make objects immutable: ```js const obj = { a: 1 }; obj.a = 2; // OK (mutating the object is allowed) obj.b = 3; // OK obj = {}; // TypeError (reassigning the variable is not) `` For true immutability, use `Object.freeze(obj)`. ### `const` Must Be Initialized ```js const z; // SyntaxError: Missing initializer in const declaration const z = 5; // OK `` `let` can be declared without initialization (it gets `undefined`). ### Block Scope ```js if (true) { let a = 1; const b = 2; } console.log(a); // ReferenceError console.log(b); // ReferenceError `` Both `let` and `const` are confined to their block. `var` would leak out. ### Hoisting and the TDZ `let` and `const` are hoisted (memory is allocated) but they sit in the **Temporal Dead Zone** until the declaration line: ```js console.log(x); // ReferenceError: Cannot access 'x' before initialization let x = 5; `` This is the key difference from `var`, which is hoisted with `undefined`. ### No Re-Declaration ```js let x = 1; let x = 2; // SyntaxError: Identifier 'x' has already been declared `` `var` allows re-declaration silently. `let` and `const` throw. ### `let` in Loops ```js for (let i = 0; i < 3; i++) { setTimeout(() => console.log(i), 0); } // logs 0, 1, 2 `` Each iteration gets a fresh binding of `i`. This fixes the classic `var` closure bug. ### When to Use Each - **`const`** by default (most variables should not be reassigned). - **`let`** when you need to reassign (counters, loop variables that change). - **`var`** only in old code or when you specifically need function scoping (rare). ### The Takeaway `let` and `const` are block-scoped, respect the TDZ, and do not allow re-declaration. `const` prevents reassignment but does not freeze objects. Use `const` by default, `let` for reassignable variables, and avoid `var`.
let allows reassignment; const does not. Both are block-scoped and in the TDZ until their declaration line. const must be initialized at declaration; let can be declared without a value (it gets undefined).
No. const prevents reassignment of the variable, but you can still mutate the object's properties. Use Object.freeze() for true immutability.
Memory is allocated for them (so technically yes), but they sit in the Temporal Dead Zone until the declaration line. Accessing them before the line throws ReferenceError, unlike var which is hoisted with undefined.
No. Redeclaring let or const in the same scope throws SyntaxError: Identifier 'x' has already been declared. var allows re-declaration silently, which is one reason to avoid var.
Use const by default (most variables should not be reassigned). Use let when you need to reassign (counters, changing loop variables). Avoid var in modern code; it is function-scoped and hoisted with undefined, which causes bugs.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

