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
Ben Myhre
28,726 PointsLooking for some authoritative voices AND sources on the assertion to avoid Var.
I can find plenty of blog posts and stack overflow answers that indicate as much, but I would be interested if I could find in MDN or ES6 rules themselves... or those that write these things... that indicate that var should be avoided.
While I can show the examples of WHY and show blog posts, having the additional support of authority would help me sell this.
Any such resources would be greatly appreciated!
2 Answers
Evan Demaris
64,262 PointsMDN and ECMAScript both indicate uses for let as well as var, and don't preclude the use of var.
Steven Parker
243,656 PointsI'm not sure you will find this in any official "rules".
Certainly, there will be times when you intentionally want to create a variable in global or function scope. But otherwise, it would certainly qualify as a "best practice".
To whom do you need to "sell" this?
Ben Myhre
28,726 PointsNot necessarily sell, but bring to the team to try and help steer our best practices for new code.
Ben Myhre
28,726 PointsBen Myhre
28,726 PointsIt does seem, however, that the general sentiment (including the video that spawned this question) is to avoid using var. Like anything, I would imagine there is a time and place for var, but I guess that is what I am trying to explore. Thank you for the links!
Evan Demaris
64,262 PointsEvan Demaris
64,262 PointsYou are completely correct in the general sentiment.
letis new, more precise (preventing scope pollution/unintentional changes being made to variables), and to my understanding can be used exclusively instead of var.To open that exploration, though - if you want a variable to be available globally instead of scoped locally, it may be more sensible to declare it as a var instead of declaring it as a let in the global scope then updating it in the local scope. Personally however, I use let almost exclusively because it prevents that accidental variable reuse - such as multiple loops running through
iat the same time with each incrementing it when only one is intended to at a time, for example.