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 trialAdam Rubinson
4,286 Pointsconst 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
231,269 PointsWhat 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
Full Stack JavaScript Techdegree Student 11,638 PointsYeah 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
231,269 PointsSure, 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
Full Stack JavaScript Techdegree Student 11,638 PointsThanksSteven .
Its my mistake , I must have to write the whole question to avoid any confusion .
Next time , I will mind that. :)
Masha Blair
13,005 PointsGreat question and great answer. Thank you, Steven Parker!
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsBy 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
variablestatus
whose value is initiallyfalse
, if the user become online , we can update thestatus
variable status , by updating it totrue
.Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou 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.