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.