Difference between let, const, and var
Learn how variable declarations differ in scope and behavior in modern JavaScript.
Advertisement
Question
What’s the difference between let, const, and var?
Answer
In JavaScript:
varis function-scoped, hoisted, and can be re-declared.letandconstare block-scoped and introduced in ES6.constmust be initialized and cannot be reassigned.
if (true) {
var a = 10;
let b = 20;
const c = 30;
}
console.log(a); // âś… 10
console.log(b); // ❌ ReferenceError
console.log(c); // ❌ ReferenceError
Hoisting Difference:
var declarations are hoisted with undefined, while let and const are hoisted but remain in the temporal dead zone until initialized.
Real-World Example
Use const for constants, let for values that change, and never use var unless maintaining legacy code.
Quick Practice
Predict the output:
console.log(a);
var a = 5;
Now replace var with let. What happens?
Summary
Use const by default, let for mutable bindings, and avoid var. It’s 2025 — no excuses.
Which should I use most often?
Prefer const by default, and use let only when you need to reassign the variable.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement