In what ways can we access the value of an object property?


Say you have this object:

const dog = {
  name: 'Roger'
}

Get valuenameFor attributes, you can use dot syntax, as shown below:

dog.name

Or you can useSquare bracket attribute accessorsyntax:

dog['name']

When the attribute does not have a valid variable name (for example,the name:

const dog = {
  'the name': 'Roger'
}

dog[‘the name’]

It is also very useful when you don't know the property name in advance and evaluate it programmatically, for example in this example looping through the object property names:

const dog = {
  'the name': 'Roger'
}

for (const [key, value] of Object.entries(dog)) { console.log(value) }

Download mine for freeJavaScript beginner's manual


More js tutorials: