2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244c7
2021-02-06 04:42:36 +00:00
title: Accessing Object Properties with Dot Notation
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cGryJs8'
forumTopicId: 16164
2021-01-13 03:31:00 +01:00
dashedName: accessing-object-properties-with-dot-notation
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
There are two ways to access the properties of an object: dot notation (`.` ) and bracket notation (`[]` ), similar to an array.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Dot notation is what you use when you know the name of the property you're trying to access ahead of time.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here is a sample of using dot notation (`.` ) to read an object's property:
2020-04-29 18:29:13 +08:00
```js
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
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` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`hatValue` should be a string
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(typeof hatValue === 'string');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The value of `hatValue` should be `"ballcap"`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(hatValue === 'ballcap');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`shirtValue` should be a string
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(typeof shirtValue === 'string');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The value of `shirtValue` should be `"jersey"`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(shirtValue === 'jersey');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use dot notation twice
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/testObj\.\w+/g).length > 1);
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --after-user-code--
```js
(function(a,b) { return "hatValue = '" + a + "', shirtValue = '" + b + "'"; })(hatValue,shirtValue);
```
## --seed-contents--
```js
// Setup
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
// Only change code below this line
var hatValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
var hatValue = testObj.hat;
var shirtValue = testObj.shirt;
```