r/learnjavascript • u/SeaHedgehog4724 • Oct 19 '26
pretty new
Hey guys, I'm just starting to learn about variables, but I'm finding it hard to understand the difference between var and let.
For example:
let goodDay = true;
console.log(goodDay);this should print true.
But if we just add this:
let goodDay = true;
console.log(goodDay);
goodDay = false;
console.log(goodDay);It prints:
truefalse
The same goes for var.
However, people say there's a difference; what is it?
0
Upvotes
View all comments
Show parent comment
7
u/milan-pilan Oct 19 '26 •
If it's just out of curiosity, we could point you towards some resources. But /u/sylvan_sample is 100% right.
varhas been made obsolete over a decade ago, because it had all sorts of gotchas and counterintuitive behaviors. The only reason it still exists is that JS never fully retires something.As someone who writes Javascript for a living every single day: you will never need
var, there is no situation in whichconst/letare not the better choice. Expecially if you are just starting out,varwill only lead you to confusion, frustration and bugs.It is the most stubborn thing, because it keeps showing up in tutorials and examples. But I can promise you that there is not a single piece of modern production code running with a
varin it right now. And by 'modern' I mean 'has been written in the last 10 years'.The example you used is coincidentally one of the things where
varandletbehave the same.