Dot operator or[]

Tell us what’s happening:
when i am trying to access myObj class properties using (.)dot operator in the function checkObj
example: return myObj.checkProp it is not returning the value but myObj[checkProp] is returning correct.

Your code so far

// Setup
var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
 if(myObj.hasOwnProperty(checkProp))
  return myObj[checkProp];
  else return "Not Found";
}

// Test your code by modifying these values
checkObj("pet");

**Link to the challenge:**
https://www.freecodecamp.org/challenges/testing-objects-for-properties

Yes, that’s the whole reason why the [] notation is supported. Your myObj does not have a checkProp property, so undefined is returned.

1 Like