Stuck on Accessing Object Properties with Dot Notation

Below’s my code. I’ve also tried putting “ballcap” and “jersey” in quotes which works as long as I don’t have testObj. in the variables. When I include testObj in with the strings I’m told that it’s expecting an identifier, without telling me what an identifier is.

Your code so far


// Setup
var testObj = {
  "hat": "ballcap",
  "shirt": "jersey",
  "shoes": "cleats"
};

// Only change code below this line

var hatValue = testObj.ballcap;      // Change this line
var shirtValue = testObj.jersey;    // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-dot-notation

There is nothing called “ballcap” or “jersey” in the object testObj that you can look up.

An object works like a dictionary: if you look up a word, it gives you the definition.

So given the word cat, I would search the dictionary for cat, I would not search the dictionary for a small domesticated carnivorous mammal with soft fur, a short snout, and retractile claws

1 Like

An object is a list of key=value pairs, but the JavaScript notation is key:value. Given a key, you get the value. In accessing the values, you don’t necessarily need the keys to be in double quotes.

I don’t understand; “ballcap” and “jersey” are right there in testObj.

But…I think I see what you mean; on a whim I tried this:

var hatValue = testObj.hat;      // Change this line
var shirtValue = testObj.shirt;    // Change this line

And it worked!

See the dictionary analogy. Sure, if you want to find something in a dictionary, and you know a definition, you can search through the entire dictionary for that definition. And if there are only two words in the dictionary, sure, it will be easy to find the words by their definition: they’ll be on the same page. But that’s not how you use a dictionary.