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 trialRouillie Wilkerson
10,419 PointsLet 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
231,268 PointsSome 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
10,419 PointsRouillie Wilkerson
10,419 PointsThank you, I understand some of your response, but you've given me more reading to do! That's a good thing!
Steven Parker
231,268 PointsSteven Parker
231,268 PointsRouillie Wilkerson — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!