If youâre learning JavaScript, one of the first questions youâll face is:
đ Should I use const or let?
And whatâs up with this old thing called var?
It sounds simple, right?
But trust me â as a senior dev whoâs done countless code reviews and technical interviews, I can tell you:
this is one of those âobviousâ things that many developers still get wrong. đ
So letâs break it down â clearly, practically, and once and for all. đ
đ const â constant, but not always frozen
When you use const, youâre saying:
âThis variable name will always refer to the same thing.â
Example:
const name = "Alice";
console.log(name); // Alice
name = "Bob"; // â TypeError
You canât reassign a const.
But hereâs a fun twist: if itâs an object or array, you can still change its contents đ
const numbers = [1, 2, 3];
numbers.push(4); // â
works
console.log(numbers); // [1, 2, 3, 4]
numbers = [0]; // â can't reassign
đĄ const doesnât make the value itself immutable â only the reference (the âbox labelâ) is fixed.
If you truly need immutability, use Object.freeze(), but note itâs shallow.
â
Use const by default.
It keeps your code predictable and self-documenting â if something shouldnât change, make it const.
đ let â for things that change
When you do need to reassign a variable, go for let:
let counter = 0;
counter = counter + 1;
console.log(counter); // 1
You can also use let inside blocks (if, for, {}):
if (true) {
let message = "Hello!";
console.log(message); // works here
}
console.log(message); // â ReferenceError
Thatâs because let is block-scoped â it only exists inside the { } where it was declared.
đ§Š Bonus fact:
Both let and const are hoisted, but they live in the Temporal Dead Zone (TDZ) until the code reaches their declaration.
Thatâs why accessing them too early throws a ReferenceError:
console.log(x); // â ReferenceError
let x = 5;
đŤ var â the old-school way (for history lovers)
Before 2015, JavaScript only had var.
It kind of works like let, but with some weird, legacy behaviors.
Example:
if (true) {
var name = "Charlie";
}
console.log(name); // â
still works outside the block!
Thatâs because var has function scope, not block scope.
You can even access it before the declaration, due to hoisting:
console.log(a); // undefined
var a = 10;
And hereâs what function scope really means:
function test() {
if (true) {
var greeting = "Hi!";
}
console.log(greeting); // â
"Hi!"
}
test();
console.log(greeting); // â ReferenceError
đ§ Nowadays, we use let and const instead â but itâs still good to know what var does.
Youâll definitely encounter it in older codebases.
đ§ Quick Summary
| Keyword | Can Reassign? | Scope | Hoisted? | Use It? |
|---|---|---|---|---|
const |
â No | Block | â (TDZ) | â Default |
let |
â Yes | Block | â (TDZ) | â When value changes |
var |
â Yes | Function | â
(initialized as undefined) |
đŤ Avoid (legacy) |
đĄ Pro Tip
In modern JavaScript projects (especially with ESLint), youâll often see a rule that says:
âUse const wherever possible.â
Thatâs not dogma â itâs good hygiene.
It keeps your codebase more predictable and helps tools catch bugs early. đ§ââď¸
đ Wrap Up
So, in short:
đ˘ Use const most of the time
đĄ Use let if you really need to change the value
đ´ Avoid var unless youâre maintaining ancient code
Keep your variables tidy, your scope clear, and your code modern! â¨
đŹ Your turn:
Do you ever still use var?
Or maybe youâve seen a weird hoisting bug in the wild?
Drop it in the comments â letâs swap battle stories đ
Top comments (4)
Great article! The const vs let breakdown is clear and practical â perfect for beginners who often mix them up.
Thank you! Always happy to help đ
Youâre most welcome, Sylwia! Really enjoyed the way you explained the const vs let logic â simple but deep. Looking forward to more of your posts!
Great, I learned a lot from this post, thank you.