What's a difference between var and let?

So I clearly see the difference with those two and const. But it’s kinda confusing to determine when to use var and when to use let. They both can change value later and can be assigned without value right? Then whats a difference and why programmer prefer let than var?
Also, if you don’t want to change value later you can just not touch it anymore, but what’s a reason why you really need const that would do same thing?

Scope is the short answer. var isn’t used as expansively as it maybe once was. Only use it when you have a very specific need to use it.

See this discussion and other for more background (talking about scope): javascript - What's the difference between using "let" and "var"? - Stack Overflow

As for why you need const, it’s a safety thing. You guarantee that nothing (or nobody, including future self if you forget the nuances of your code) changes a crucial “pillar” of your code.

4 Likes

Hey there!
What @toastedpitabread said, and also: let and const were introduced only in 2015 with ES6, you’ll see that the global usage of var is slightly wider than the other two, all of them are supported above 90% of all browser versions.

More info about this on caniuse.com: