1

Hi guys I have just started learning to code and I have hit a road block in accessing the properties of the object I have created.

Here is my object.

var restaurantOrder = {
  "my entree": "cheeseburger",
  "my side": "fries",
  "the drink": "water"
};

I would like to get the value of entree however nothing I am trying seems to work :(

Here is what I have tried.

var entreeValue = restaurantOrder.my entree;
var entreeValue = restaurantOrder[my entree];
var entreeValue = restaurantOrder.[my entree];
var entreeValue = restaurantOrder.["my entree"];

None of the above lines work :( Thank you for your help.

1 Answers1

0

Because the properties of your restaurantOrder object have spaces, you will need to use [] to access them. You cannot use . like you can with a property name that is a single word.

Also, because of the spaces you will need to enclose the property name with quotes, so either:

var entreeValue = restaurantOrder["my entree"];

or

var entreeValue = restaurantOrder['my entree'];

will work.

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40