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

Whats the right answer for this question? Whichever answer I choose, it says am wrong.

Which of these is NOT the right way to access property "name" on an Object?

cat.name

cat["name"]

var key="name";cat.key

var key="name";cat[key]

1 Answer

Jacob Tollette
Jacob Tollette
27,884 Points

As Robert said, the third option is NOT the right way to access the property.

Option 1 is normal dot notation for accessing properties.

Option 2 would be normal square-bracket notation.

Option 4 is just another way to do square-bracket notation and would read like this to the interpreter:

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

Option 3 would be some unholy crossbreeding of both and would read something like this to the interpreter:

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

I hope that clears things up.