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 trialJeremy Castanza
12,081 PointsLet vs. Const for username
Just wanted to get a second opinion for the User Route video. Andrew Chalkley uses "var" to declare the username in the userRoute function.
With newer versions of ECMAScript, I know they're trying to phase out "var". In this example, would it make sense to use "let" since usernames would vary?
I'm still not 100% clear on when to use const and let.
Jeremy Castanza
12,081 PointsTimothy Acker , is it best practice to just use const on objects or is there a time when using let would be preferred?
4 Answers
Stephan L
17,821 PointsThis course explains let and const in ES6 in more detail: https://teamtreehouse.com/library/introducing-es2015
But generally, let is used when the value of the variable will change (e.g., it is used as an iterable, it's a value that's populated programmatically or its value is based on user input). const is used when a value is going to be "constant" meaning it won't be re-declared at some point later (you'll actually throw an error if you try to re-declare a const variable). Using let or const can also signal to people reading your code later which variable values are going to change and which are not.
Hope that helps.
Tri Pham
18,671 PointsUse const as a default. And then use let when you actually need to reassign the variable later (it'll throw an error so you can't really screw up on that).
Adam Fields
Full Stack JavaScript Techdegree Graduate 37,838 Pointsconst http = require('http')
In this case, the variable http
will ALWAYS be the same. http
will always have the value require('http')
.
In a for-loop, the value of i
is CHANGING so you must use let
:
for (let i = 0; i < num; i ++) {
...
}
My contribution to this discussion is to learn how to use and configure ESLint. The ESLint rules no-var
and prefer-const
will remind you what to use. If you try to mutate a const
, you'll get an error. Likewise, if a let
never changes, it'll ask you to change it to a const
.
Also worth mentioning that I have seen ESLint mentioned in job descriptions, so it's definitely a useful tool to add to your arsenal.
As for Andrew's usage of var
in this video, keep in mind many of the Treehouse videos are a few years old. The new videos use ES2015 syntax or JSX if it's React.
Tim Acker
Front End Web Development Techdegree Graduate 31,247 PointsGreat question. From what I've learned so far that seems to be the case, but I'm not certain. I would like some clarification on that as well.
Tim Acker
Front End Web Development Techdegree Graduate 31,247 PointsTim Acker
Front End Web Development Techdegree Graduate 31,247 PointsYes, you would need to use let if the usernames change as const variables cannot be updated. If the const is set to an object, you can, however, change its properties. So, for example, if the const is set to an element in the DOM, you can change its font-size or its innerHTML.