Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Introducing Variables

Frans de Haan
Frans de Haan
6,315 Points

Using let instead of var

Hello,

Why we don't use let instead of var. I think this is the better and newer way?

I agree

4 Answers

Indeed let is the newer and better way. However, this video was made before let was introduced :laughing: Besides, if you are just learning JS, you want to know var before let.

Is there any difference between those two?

There are certainly differences between the old way of declaring variables in JS, using var, and the new way, using let and const. I highly recommend googling it, but i'll try to explain a little bit of it. It mostly has to do with Global vs block level scope. I believe this course will explain scope later. But essentially a common issue with var is that, if you're not careful, you may unintentially change the value of your variable at some point. Since var has no way to lock in it's value they (The mystics of JS magic) decided to create 2 new ways to declare variables, which are to replace var. Var will still work, but you should learn the 2 new ways. let acts pretty similar to var, with some key difference. The second way of declaring variables is now 'const'. Const solves the issue of mistakenly changing the value of variables. Const variable cannot have their value changed. A good example of when to use let or const would be, for example, your score in a video game always changes, so use let. A mathematical equation, or the starting score in a video game are always the same, so use const. Hope this helps. Remember, use all resources. Especially google for clarification. Keep up the good work, Remco.

-Thomas Ketterman

Thank you Thomas for your clarifications.

thank you Thomas