Accessing Objects Properties with the Dot Operator

I think i might be passing this in the wrong way…make sense? Im not very good with javascript or terminology but I dont think your supposed to “tell” the .prop what it is equal to but its passing…seems in other situations this would be a poor solution.

// Setup
var testObj = {
“hat”: “ballcap”,
“shirt”: “jersey”,
“shoes”: “cleats”

// Only change code below this line

var hatValue = testObj.prop1 = “ballcap”; // Change this line
var shirtValue = testObj.prop2 = “jersey”; // Change this line

They are asking you to reference those values. Hat and Shirt are the keys and they want you to return their respective values of ballcap and jersey. There are two ways to do that, dot notation:

var hatValue = testObj.hat; 
var shirtValue = testObj.shirt;

or with bracket notation:

var hatValue = testObj['hat'];
var shirtValue = testObj['shirt']; 

Either will assign those respective values.

appreciate it man i was way off, makes sense way var hatValue = testObj.ballcap; didnt work.

Right, hat is the key, and ballcap is the value. The keys are fixed - every one has a “shirt” but the value tells you what kind of shirt.

It’s kind of funny that your “solution” kind of worked. It was just assigning the value from right to left;

var hatValue = testObj.prop1 = "ballcap";

“ballcap” gets assigned to testObj.prop1. testObj doesn’t have a prop1 so JS creates it. Then that value (still “ballcap”) gets assigned to hatValue.

Funny that it basically ended up doing what it was supposed to do by accident.

@ksjazzguitar please note that bracket notation is used only if there is a space within the propp name/declaration(i learned that the hard way)

Not true, it can be used, space or no space. In fact, for a number brackets must be used as the dot notation does not work with numbers.

For example, it the setup was:

// Setup
var testObj = {
"1": "ballcap",
"2": "jersey",
"3": "cleats"

var hatValue = testObj.1;  // does not work
it would have to be:
var hatValue = testObj["1"]; // works

What you mean is that if there is a space in the key, you must use bracket notation.

yeah but i tried that on one of the challenges and it drove me crazy till it finally worked and the issue was the bracket notation. i kept using it with the variable name until finally i came to the forum and there i figured out the answer

But that’s not what you wrote. You wrote “bracket notation is used only if there is a space within the propp name/declaration…” which is misleading and incorrect.

sorry my bad i messed up

That’s cool. I’m still learning myself. I had to correct you because someone who doesn’t know any better may read your comment and think it’s true.

i dont blame i am still in middle school and it is one of the most delightful moments correcting my teachers in front of the class and also because it is quite hard to sit there listening to false info too. anyways thanks for correcting me and now i and everyone else will know better next time.

1 Like