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 Build a Simple Dynamic Site with Node.js Handling Routes in Node.js User Route

Jeremy Castanza
Jeremy Castanza
12,080 Points

Let 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.

Timothy Acker
.a{fill-rule:evenodd;}techdegree seal-36
Timothy Acker
Front End Web Development Techdegree Graduate 31,247 Points

Yes, 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.

Jeremy Castanza
Jeremy Castanza
12,080 Points

Timothy 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
Stephan L
17,821 Points

This 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
Tri Pham
18,671 Points

Use 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).

const 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.