Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Rouillie Wilkerson
Rouillie Wilkerson
10,407 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
210,850 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,407 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
210,850 Points

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