2018-09-30 22:01:58 +00:00
---
id: 56533eb9ac21ba0edf2244c8
title: Accessing Object Properties with Bracket Notation
challengeType: 1
2019-02-14 17:24:02 +00:00
videoUrl: 'https://scrimba.com/c/cBvmEHP'
2019-07-31 18:32:23 +00:00
forumTopicId: 16163
2021-01-13 02:31:00 +00:00
dashedName: accessing-object-properties-with-bracket-notation
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
The second way to access the properties of an object is bracket notation (`[]`). If the property of the object you are trying to access has a space in its name, you will need to use bracket notation.
2018-09-30 22:01:58 +00:00
However, you can still use bracket notation on object properties without spaces.
2020-11-27 18:02:05 +00:00
2018-09-30 22:01:58 +00:00
Here is a sample of using bracket notation to read an object's property:
2019-05-17 13:20:30 +00:00
```js
var myObj = {
"Space Name": "Kirk",
"More Space": "Spock",
"NoSpace": "USS Enterprise"
};
2021-03-03 00:12:12 +00:00
myObj["Space Name"];
myObj['More Space'];
myObj["NoSpace"];
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
`myObj["Space Name"]` would be the string `Kirk` , `myObj['More Space']` would be the string `Spock` , and `myObj["NoSpace"]` would be the string `USS Enterprise` .
2018-09-30 22:01:58 +00:00
Note that property names with spaces in them must be in quotes (single or double).
2020-11-27 18:02:05 +00:00
# --instructions--
2021-03-03 00:12:12 +00:00
Read the values of the properties `an entree` and `the drink` of `testObj` using bracket notation and assign them to `entreeValue` and `drinkValue` respectively.
2020-11-27 18:02:05 +00:00
# --hints--
`entreeValue` should be a string
```js
assert(typeof entreeValue === 'string');
2018-09-30 22:01:58 +00:00
```
2021-03-03 00:12:12 +00:00
The value of `entreeValue` should be the string `hamburger`
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(entreeValue === 'hamburger');
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`drinkValue` should be a string
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert(typeof drinkValue === 'string');
```
2018-09-30 22:01:58 +00:00
2021-03-03 00:12:12 +00:00
The value of `drinkValue` should be the string `water`
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(drinkValue === 'water');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
You should use bracket notation twice
```js
assert(code.match(/testObj\s*?\[('|")[^'"]+\1\]/g).length > 1);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2018-10-20 18:02:47 +00:00
(function(a,b) { return "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
## --seed-contents--
```js
// Setup
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
// Only change code below this line
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
var entreeValue = testObj; // Change this line
var drinkValue = testObj; // Change this line
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
var entreeValue = testObj["an entree"];
var drinkValue = testObj['the drink'];
```