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
177,578 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
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,610 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
177,578 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
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,610 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
177,578 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.

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,610 Points

ThanksSteven .
Its my mistake , I must have to write the whole question to avoid any confusion .
Next time , I will mind that. :)

Masha Blair
Masha Blair
12,997 Points

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