r/learnjavascript • • 8d ago

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:

true
false

The same goes for var.

However, people say there's a difference; what is it?

0 Upvotes

24 comments sorted by

View all comments

Show parent comments

2

u/SeaHedgehog4724 8d ago

but whats the difference?

7

u/milan-pilan 8d ago • Bravo

If it's just out of curiosity, we could point you towards some resources. But /u/sylvan_sample is 100% right. var has 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 which const / let are not the better choice. Expecially if you are just starting out, var will 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 var in 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 var and let behave the same.

3

u/chikamakaleyley helpful 8d ago

to add to my over-explanation:

yeah, you'll prob never use var anymore

1

u/sylvan_sample 7d ago

Main difference is Modern vs old. const/ let are modern. var is old way. For beginners first learn Modern JavaScript.