Nested Objects in JavaScript

What are the nested objects in this code? If its desk cabinet and top drawer why do they look like the property side of a key/value pair? It’s confusing to see a nested object not look the object ourStorage.

Thanks

const ourStorage = {
  "desk": {
    "drawer": "stapler"
  },
  "cabinet": {
    "top drawer": { 
      "folder1": "a file",
      "folder2": "secrets"
    },
    "bottom drawer": "soda"
  }
};

ourStorage.cabinet[“top drawer”].folder2;
ourStorage.desk.drawer;

Tell us what you think are the objects in the object ourStorage.

Like i said,I think the objects are desk, cabinet, and top drawer. I think that because they are set up before the curly brackets that have key/value pairs inside but that doesn’t seem right because they look like the property side of a key value pair?

That is correct. These are all nested object of ourStorage. Objects have properties which are propertyName/value pairs. A property itself can be an object. This means that the value (the right side of the propertyName/value pair) is an object.

1 Like

So are there more objects in this code? Can you also consider drawer, folder1, folder 2, and bottom drawer objects? Or does the fact that property is assigned a value make that just a property of an object and not object. Or is that all properties are also in fact objects? Is Bottom drawer a nested object/property of the object ourStorage with the value of soda?

Or does the bracket notation determine if a property is an object?

Thanks

Objects are made up of property names and values for those property names. Together, they are considered properties of the object. A property’s value can be anything (Number, String, Array, Object, etc.). The property which has the name desk contains an object as its value. How do I know it is an object? It is surrounded by { and } and has properties.

The property named drawer which is a property of the object which is named desk contains a String as its value. The properties represented by the names folder1, folder2 and bottom drawer also contain Strings as their values.

1 Like