Accessing Object Properties with Dot Notation
There are two ways to access the properties of an object: dot notation (.
) and bracket notation ([]
), similar to an array.
Dot notation 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 dot notation (.
) to read an object's property:
const myObj = {
prop1: "val1",
prop2: "val2"
};
const prop1val = myObj.prop1;
const prop2val = myObj.prop2;
prop1val
would have a value of the string val1
, and prop2val
would have a value of the string val2
.
Read in the property values of testObj
using dot notation. Set the variable hatValue
equal to the object's property hat
and set the variable shirtValue
equal to the object's property shirt
.
Tests
- Waiting: 1.
hatValue
should be a string - Waiting: 2. The value of
hatValue
should be the stringballcap
- Waiting: 3.
shirtValue
should be a string - Waiting: 4. The value of
shirtValue
should be the stringjersey
- Waiting: 5. You should use dot notation twice
/** * Your test output will go here */