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