/* so guys pleas help me on this!!
am having a hard time trying to understand why is it when i use …
return obj.prop the console output is ….undefind, but if i use obj[prop]
the console output is Brian Oneal,
This evaluate the property named “prop” on object obj.
If you are hard coding the property name, you can use dot notation:
const obj = {
foo: 'bar',
}
console.log(obj.foo)
// bar
If you don’t know what actual property name is, like it is in a variable, you have to use bracket notation:
const obj = {
foo: 'bar',
}
const propName = 'foo'
console.log(obj[propName])
// foo
console.log(obj.propName)
// undefined, because there is no property named "propName"
The other time you have to use bracket notation is if the property name is not a valid JS identifier, e.g., it starts with a number, has a hyphen in it, has a space in it, etc.
const obj = {
'my-name': 'Kevin',
}
console.log(obj['my-name'])
// Kevin
console.log(obj.my-name)
// gives you an error
Notice that when I declared the object, I also had to wrap the property name in quotes, for the same reason.