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 Object-Oriented JavaScript (2015) Introduction to Methods Object Literal Review

The video said a key accessed by [] must be in quote marks. Answer D has cat[key] which is not in quote marks.

The question and answers are ambiguous and confusing. The question asks about accessing the key 'name' yet it said D was correct because it use bracket method.. but that was on cat[key]. What does that have to do with "name"?

I think A and D are the correct answers but especially D since in cat[key], "key" is not in quote marks. Shouldn't it be to be valid?

PS. While writing this note, whenever I hit Enter it clicked on the the Next button. And if I was typing over an answer, it thought I chose that answer. I ended up failing the quiz. The behavior on this message box was also going to the page below it. Pretty big bug, no?

1 Answer

andren
andren
28,558 Points

You seem to have misunderstood a couple of things.

First of all you don't need to use strings when using bracket notation if the property has a valid variable name. For example if the property is called age then quotes would not be needed to access it because it is a valid variable name. If the property was named Max Height then quotes would be needed because it is invalid to have a space in a variable name.

It's worth mentioning that for these questions Treehouse actually randomizes the order that the choices appear in. So referencing choice A and D is not actually very useful because what those letters refers to is randomized each time you take the test.

Anyway I assume that by choice D you refer to this choice:

var key = "name"; cat[key]

The reason that is valid is that key is a variable which has been set to name, and when you use bracket notation it is valid to use variable names to access the properties.

The only invalid way to reference the property of those listed is this:

var key = "name"; cat.key

Because when using dot notation variables are not valid, you have to use the actual name of the property.

It's also worth mentioning that it is highly discouraged to use property names which are not valid variable names, exactly because they require you to use quote marks to access them and because it makes them inaccessible using dot notation. So in real applications you will almost never see anybody using property names with spaces and the likes, and as such will almost never see people using quotes when accessing properties either.