2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244c8
2021-02-06 04:42:36 +00:00
title: Accessing Object Properties with Bracket Notation
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cBvmEHP'
forumTopicId: 16163
2021-01-13 03:31:00 +01:00
dashedName: accessing-object-properties-with-bracket-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
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.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
However, you can still use bracket notation on object properties without spaces.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Here is a sample of using bracket notation to read an object's property:
2020-04-29 18:29:13 +08:00
```js
var myObj = {
"Space Name": "Kirk",
"More Space": "Spock",
"NoSpace": "USS Enterprise"
};
myObj["Space Name"]; // Kirk
myObj['More Space']; // Spock
myObj["NoSpace"]; // USS Enterprise
```
2021-02-06 04:42:36 +00:00
Note that property names with spaces in them must be in quotes (single or double).
2018-10-10 18:03:03 -04:00
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 the values of the properties `"an entree"` and `"the drink"` of `testObj` using bracket notation and assign them to `entreeValue` and `drinkValue` respectively.
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
`entreeValue` should be a string
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(typeof entreeValue === 'string');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The value of `entreeValue` should be `"hamburger"`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(entreeValue === 'hamburger');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`drinkValue` should be a string
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(typeof drinkValue === 'string');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The value of `drinkValue` should be `"water"`
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(drinkValue === 'water');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use bracket 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\s*?\[('|")[^'"]+\1\]/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 "entreeValue = '" + a + "', drinkValue = '" + b + "'"; })(entreeValue,drinkValue);
```
## --seed-contents--
```js
// Setup
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
// Only change code below this line
var entreeValue = testObj; // Change this line
var drinkValue = testObj; // Change this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
var testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};
var entreeValue = testObj["an entree"];
var drinkValue = testObj['the drink'];
```