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
var myObj = {
prop1: "val1",
prop2: "val2"
};
var prop1val = myObj.prop1; // val1
var prop2val = myObj.prop2; // val2
```
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
```
2020-11-27 19:02:05 +01:00
The value of `hatValue` should be `"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
2020-11-27 19:02:05 +01:00
The value of `shirtValue` should be `"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
var testObj = {
"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
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
var hatValue = testObj; // Change this line
var shirtValue = testObj; // Change this line
```
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
var testObj = {
"hat": "ballcap",
"shirt": "jersey",
"shoes": "cleats"
};
2018-10-08 01:01:53 +01:00
var hatValue = testObj.hat;
2018-09-30 23:01:58 +01:00
var shirtValue = testObj.shirt;
```