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

JavaScript Getting Started With ES2015 Defining Variables With let and const Using Constants with Arrays and Objects

Adam Rubinson
Adam Rubinson
4,286 Points

const usage

I have an issue with one of the questions in the question set following this video.

Q: Which of the following variables should NOT be defined using the const keyword.

A: A variable used to track a user's login status.

But technically, a const variable could be used for this purpose by assigning an object literal to the const variable and then changing a "key" inside the object, for example:

const login_info = {user: "Adam", status: "logged_in"};

and then later we use:

login_info.status = "logged_off";

Or is this considered bad practice?

4 Answers

Steven Parker
Steven Parker
231,275 Points

What you are suggesting would certainly be acceptable, but it's a bit beyond the intent of the question. Perhaps the question answer should be re-worded to something like "A variable holding a boolean value used to track a user's login status."

You may want to report this as a bug as described on the Suport page. You might get the "Exterminator" badge.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

By saying , A variable holding a boolean value used to track a user's login status." , you mean that , const keyword cant be used for a A variable used to track a user's login status.?
I didn't got that. Can you help me?
I think if there is a const variable status whose value is initially false , if the user become online , we can update the status variable status , by updating it to true.

Steven Parker
Steven Parker
231,275 Points

You can never update the value of a "const". If it is originally given the value of false, it will have that value for as long as it exists. This immutability is what makes a "const" different from a "let" variable. Even attempting to re-assign a "const" will cause your program to stop with a fatal error.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Yeah you are right. I understood that. But what I was asking was that variable status is defined as given below:

 const login_info = {user: "Adam", status: false};

And later we use:

login_info.status = true;

Is it correct?

Steven Parker
Steven Parker
231,275 Points

Sure, that works because you're only changing a property of the object, not the object itself. That's why I suggested the question be reworded to explicitly refer to a simple value to avoid any confusion.

Masha Blair
Masha Blair
13,005 Points

Great question and great answer. Thank you, Steven Parker!