Using Brackets[] and dots(.)to access properties in an object

/* 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,

:sob: :sob:


const myFamily={
“me”:“Brian Oneal”

function testObject(obj,prop){
return obj.prop;
else { return “Not found”;



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',
// 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'

// foo

// 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',

// Kevin

// 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.

thanks . :100:…fam
wished free code camp had explained it the way you did, it would make thinks much smoother in here

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