Accessing properties that are strings?

Tell us what’s happening:
Describe your issue in detail here.

tekkenCharacter['hair color'] = 'dyed orange';

Bracket notation is required if your property has a space in it or if you want to use a variable to name the property. In the above case, the property is enclosed in quotes to denote it as a string and will be added exactly as shown. Without quotes, it will be evaluated as a variable and the name of the property will be whatever value the variable is. Here’s an example with a variable:

const eyes = 'eye color';

tekkenCharacter[eyes] = 'brown';

After adding all the examples, the object will look like this:

{
  player: 'Hwoarang',
  fightingStyle: 'Tae Kwon Doe',
  human: true,
  origin: 'South Korea',
  'hair color': 'dyed orange',
  'eye color': 'brown'
};

If you add a property as a string, can you only access it through bracket notation?

  **Your code so far**

let foods = {
apples: 25,
oranges: 32,
plums: 28
};

// Only change code below this line

// Only change code above this line

console.log(foods);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

Challenge: Add Key-Value Pairs to JavaScript Objects

Link to the challenge:

Hi @drsullivan93,

It depends, if there is space, bracket notation is the only way you can use. If it is without space, you can use the dot notation. But in fact, you can access everything with the bracket notation. I use most of the time that one.

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