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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Rouillie Wilkerson
Rouillie Wilkerson
10,419 Points

Let vs Var...

I'm supplementing my Javascript learning with various other sources. One consistency is a preference for "let" instead of "var". I see "var" used here a lot. When should I use let?

1 Answer

Steven Parker
Steven Parker
230,274 Points

Some of the videos have not been revised for a while. Since ES6 (2015), "let" and "const" have become the common choices for variable declarations. "Const" is preferred when possible, and "let" if the variable will be changed.

Just be aware that they both have block scope, and are not hoisted. Use "var" if you need hoisting or function scope (but with good planning, neither should be necessary).

I'll admit that personally, I still like to use "var" for global variables just as a way to distinguish them at first glance.

Rouillie Wilkerson
Rouillie Wilkerson
10,419 Points

Thank you, I understand some of your response, but you've given me more reading to do! That's a good thing!

Steven Parker
Steven Parker
230,274 Points

Rouillie Wilkerson — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!