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 trialgene c
13,630 PointsSo do I use "var" or "let" to declare variables now?
In a previous video in the same track, I was taught to avoid using "var" to declare variables as "var" had some scope issues.
Now this video is using "var" to declare variables. So I am confused.
6 Answers
Daniel Rivers
2,931 PointsI generally start with const by default now, but decision between var and let is really about scope, I often use lets inside a loop or a function where the scope is only to that part of the code.
If I need more global scope and a changing variable I will use var.
Steven Parker
231,269 PointsIf you need function scope or hoisting, you'd need "var". But if you rethink your code structure, you can probably change it so neither of these is required. And I personally like using "var" for global variables to visually distinguish them.
Otherwise, using "const" for things that do not change, and "let" for things that do are good practice choices.
Alexander Davison
65,469 PointsAgreed
tomd
16,701 PointsYou should use 'const'. Unless you need to change the value of a variable later on, then use 'let'.
'Var' is still being used in TreeHouse videos because the videos are old. But they are still relevant.
Daniel Vazquez
Courses Plus Student 7,905 PointsAs I understood, var should be always be avoided from now on...
- const should be your first option, and if you don't want your variable to be overwritten later on.
- let is for when te value of your variable is going to be overwritten altogether.
Kevin Gates
15,053 PointsTo quote Pirates of the Caribbean, "They're more like guidelines than actual rules."
Be wise, but ultimately use the best tools for the job. Remember, at the end of the day, "Done is better than perfect."
farouq siwoku
Full Stack JavaScript Techdegree Student 7,324 PointsYou should always use let when you can i.e use it more often. while var has issues with scope and its generally not in use anymore. You should also use const to declare variables cause it helps to prevent errors.
Steven Parker
231,269 PointsI think of "const" and "let" as additional (though commonly used) tools in the toolbox, but not replacements.
There are still reasons to use var; it provides function scope and hoisting, the others don't do either. Also, if you use sandbox tools like "codepen", you might find that things declared in the console with "var" work as expected, but you may have problems accessing things declared with "let" or "const".
Daniel Rivers
2,931 PointsDaniel Rivers
2,931 PointsI generally start with const by default now, but decision between var and let is really about scope, I often use lets inside a loop or a function where the scope is only to that part of the code.
If I need more global scope and a changing variable I will use var.