--- id: 56533eb9ac21ba0edf2244cc title: Accessing Nested Objects challengeType: 1 guideUrl: 'https://www.freecodecamp.org/guide/certificates/accessing-nested-objects-in-json' --- ## Description
The sub-properties of objects can be accessed by chaining together the dot or bracket notation. Here is a nested object:
var ourStorage = {
  "desk": {
    "drawer": "stapler"
  },
  "cabinet": {
    "top drawer": {
      "folder1": "a file",
      "folder2": "secrets"
    },
    "bottom drawer": "soda"
  }
};
ourStorage.cabinet["top drawer"].folder2; // "secrets"
ourStorage.desk.drawer; // "stapler"
## Instructions
Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use bracket notation for properties with a space in their name.
## Tests
```yml tests: - text: gloveBoxContents should equal "maps" testString: assert(gloveBoxContents === "maps", 'gloveBoxContents should equal "maps"'); - text: Use dot and bracket notation to access myStorage testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code), 'Use dot and bracket notation to access myStorage'); ```
## Challenge Seed
```js // Setup var myStorage = { "car": { "inside": { "glove box": "maps", "passenger seat": "crumbs" }, "outside": { "trunk": "jack" } } }; var gloveBoxContents = undefined; // Change this line ```
### After Test
```js (function(x) { if(typeof x != 'undefined') { return "gloveBoxContents = " + x; } return "gloveBoxContents is undefined"; })(gloveBoxContents); ```
## Solution
```js var myStorage = { "car":{ "inside":{ "glove box":"maps", "passenger seat":"crumbs" }, "outside":{ "trunk":"jack" } } }; var gloveBoxContents = myStorage.car.inside["glove box"]; ```