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

js: valid ways to access a property of an object

pls look at the picture of this quiz question: https://teamtreehouse.com/workspaces/36235082#/image.png why is option B a valid way to access the property "name"? πππ

Steven Parker
Steven Parker
243,318 Points

Direct workspace URL's are temporarly and only exist while you're using them. To share a workspace, use the "snapshot" feature (the camera icon in the upper right) and share the link that creates instead.

2 Answers

The image you are linking to is not actually accessible, but based on your title I'm guessing your referring to this question from the "Object Literal Review" quiz:

Which of these is NOT a valid way to access a property, "name", on an object?

//A
var key = "name";
cat[key];

//B
var key = "name";
cat.key;

//C
cat["name"];

//D
cat.name;

The order the answers appear in is randomized each time the quiz is taken, so I don't know exactly which of these options showed as "B" to you, but I'm going to guess that it was the option listed as "A" in the code above. As that is the option most likely to cause confusion.

When you use dot notation to access a property (like cat.name) JavaScript will treat the text you write after the dot as the literal name for the property you are looking for. When you use bracket notation however (like cat[key]) JavaScript will not treat the text as the name, but rather evaluate it like regular code, and it expects to be passed a string. That means that if you write in a word like key JavaScript will look for a variable called key, and then pass in the contents of that variable.

Basically if you had code like this:

var key = "name";
console.log(key);

What would be printed out? name or key? The answer is of course name, because JavaScript prints the string that the variable holds, not what the variable is named. The exact same holds true when the variable is used with bracket syntax for objects.

So when JavaScript encounters cat[key] in the above code it looks at the string contained in key and uses that as the name of the property it should access. Which is name in the above code, and that results in the name property being accessed.

If I made a wrong assumption about what question or option confused you, or you found my explanation unclear then please post a reply clarifying exactly what confused you, and I'll do my best to clarify.

Hi, The quiz link is broken, Can You repost it Plz. However, there are two valid ways to access a property of an object.
-> SYNTAX:

  1. Dot Notation = object.property
  2. Bracket or Square Notation = object['property']

-> COMPARISION: The dot notation only works with property names which are valid identifier names [spec], so basically, any name that would also be a valid variable name (a valid identifier, see also What characters are valid for JavaScript variable names?) and any reserved keyword [spec].

You should use . DOT NOTATION when you know the name of the property

Example:

var object = {};
object.property = 'whatever';

Bracket notation expects an expression which evaluates to a string (or can be coerced to a string), so you can use any character sequence as a property name. There are no limits to what a string can contain.

Use [] SQUARE OR BRACKET NOTATION when the name of the property is contained in a variable

Example:

var object = {};
var property = 'another-property';
object[property] = 'whatever';

Good Luck