freeCodeCamp Challenge Guide: Accessing Objects Properties with the Dot Operator

freeCodeCamp Challenge Guide: Accessing Objects Properties with the Dot Operator
0

#1

There are two ways to access the properties of an object: the dot operator (.) and bracket notation ([]), similar to an array.

The dot operator is what you use when you know the name of the property you’re trying to access ahead of time.

Here is a sample of using the dot operator (.) to read an object property:

var myObj = {
  prop1: "val1",
  prop2: "val2"
};
myObj.prop1; // val1
myObj.prop2; // val2

#2

Spoiler with a question.

prop1 does not equate to “prop1” in my brain, is there a reason why it is explained this way that gets around identity property (a=a)? Earlier this is clearer and specifically addressed as a distinction. Here it just seems assumed… is there a reason why?

edit:
“Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name” -FCC (2 challenges later)


#3

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

// Only change code below this line

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