Facebook Pixel

var vs let vs const: When to Use Each in JavaScript

Three ways to declare variables in JS. Here is how to choose and why const is the default in modern code.

var vs let vs const: When to Use Each in JavaScript

JavaScript has three variable declarations. Each behaves differently. Here is how to choose.

Quick Comparison

Featurevarletconst
ScopeFunctionBlockBlock
HoistingundefinedTDZTDZ
Re-assignYesYesNo
Re-declareYes (silent)No (SyntaxError)No (SyntaxError)
Initializer requiredNoNoYes
Global → windowYesNoNo

When to Use const

Default to const. Most variables should not be reassigned:

const API_URL = "https://api.example.com"; const user = { name: "Kunal" }; const handleClick = () => { ... }; `` `const` signals intent: "this binding will not change." It prevents accidental reassignment bugs. ### When to Use `let` Use `let` when you genuinely need to reassign: ```js let count = 0; count++; let total = 0; for (const item of items) { total += item.price; } let result; if (condition) { result = computeA(); } else { result = computeB(); } `` ### When to Use `var` Almost never in modern code. The only legitimate case is when you specifically need function scoping or hoisting, which is rare. In old codebases (pre-ES6), `var` is all you have. ### A Common Misconception About `const` `const` does not mean "immutable value." It means "the binding cannot be reassigned." Objects and arrays declared with `const` can still be mutated: ```js const list = []; list.push(1); // OK (mutating the array) list = [2]; // TypeError (reassigning the variable) `` For immutability, use `Object.freeze` or libraries like Immutable.js. ### Loop Variables ```js for (let i = 0; i < 3; i++) { ... } // i changes, use let for (const item of items) { ... } // item does not change, use const for (const key in obj) { ... } // key does not change, use const `` Use `let` for the loop counter (it increments). Use `const` for `for...of` and `for...in` (each iteration is a fresh binding). ### The Takeaway Use `const` by default (most variables should not be reassigned). Use `let` when you need to reassign. Avoid `var` in modern code. `const` does not freeze objects; it only prevents reassignment of the binding.

Use const by default (most variables should not be reassigned). Use let when you need to reassign (counters, changing values). Avoid var in modern code; it is function-scoped and hoisted with undefined, which causes bugs.

No. const prevents reassignment of the variable, but you can still mutate the array or object (push, pop, property changes). Use Object.freeze() or immutable libraries for true immutability.

const. Each iteration of for...of creates a fresh binding, and the loop variable does not change within an iteration. Use let only for classic for loops where the counter increments.

var is function-scoped (leaks out of blocks), hoisted with undefined (causes hoisting bugs), and allows silent re-declaration (hides typos). let and const are block-scoped, respect the TDZ, and throw on re-declaration.

const prevents reassignment of the variable binding. Object.freeze prevents mutation of the object itself (no property changes). They work at different levels: const is about the variable, freeze is about the value.

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.

Please Login.
Please Login.
Please Login.
Please Login.