Dot notation vs bracket notation in the example

Hi All

From what I’ve read so far if I want to access the data stored in a variable I must use bracket notation. Is this correct/
In the example for this exercise there doesn’t seem to be an obvious reason for one use over the other.

  **Your code so far**

// Setup
var myStorage = {
"car": {
  "inside": {
    "glove box": "maps",
    "passenger seat": "crumbs"
   },
  "outside": {
    "trunk": "jack"
  }
}
};

var gloveBoxContents = ; // Change this line
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.

Challenge: Accessing Nested Objects

Link to the challenge:

I see that its because there is a break in the string.

1 Like

As you pointed out the obvious reason to use bracket notation here is because the keys on the object have spaces so bracket notation is the only option.

Another reason would be if you want to access an item in the object dynamically.

A trivial example would be:

let key = 'inside'

console.log(myStorage.car[key])

Dot notation just won’t work in the above example.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.