#Javascript, accessing properties with bracket notation

6 messages · Page 1 of 1 (latest)

undone flower
#

Hi, I have been trying to figure out this "homework for awhile. It is from CodeAcademy, the free version.

So it is not for class like at a school, just me trying to learn on my own.

I have been tasked to these directions:

"Read the values of the properties an entree and the drink of testObj using bracket notation and assign them to entreeValue and drinkValue respectively."

Below is the code I need to change. However, I cannot figure out how to do it. If you can help, please.

const testObj = {
"an entree": "hamburger",
"my side": "veggies",
"the drink": "water"
};

//change below this line
const entreeValue = testObj;
const drinkValue = testObj;

calm knoll
#

to access things in js objects you need to either use dot notation objName.key or bracket notation objName[key]. Since there is a space in your keys, you'll need to use bracket. so since "an entree" is a key to the value "hamburger", if I wanted to access that I'd write testObj["an entree"] to grab that value

undone flower
#

Thank you! This helps.

calm knoll
#

no problem! good luck:)

undone flower
#

But how do I assign them to the entreeValue and drinkValue?

calm knoll
#

testObj[“an entree”] = “my new entree”