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

Laura Perea
Laura Perea
7,630 Points

Why use let instead of var?

I was trying to be a step ahead when watching this video – I chose to use var when setting the variable for message (to fix the error with const). Ultimately though, Guil chose let. They both work in this example with no risks that I can see. What would be better practice here? Any reason why let would generally be a wiser choice?

1 Answer

The main difference is the scope difference, while let can be only available inside the scope it's declared, like in for loop, var can be accessed outside the loop for example.

Var is also obsolete and is no longer used. You can assume that let and const are the only things you will be using.

Here is more about this topic → https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var