Testing Objects for Properties - Basic JS

Alright, so I’m not having issues with the challenge, I’m just curious about why this is behaving as such.


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

function checkObj(checkProp) {
  // Your Code Here
  let x = myObj.hasOwnProperty(checkProp);
  if (x === true) {
    return myObj.checkProp;
  }
  return "Not Found";
}

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

The above code doesn’t work. However, if I use return myObj[checkProp]; instead, it works fine. What’s the difference here?

When you write myObj.checkProp, there must be an actual property named checkProp, otherwise it is just undefined. The only properties myObj has are gift, pet, and bed. You might want to review dot vs bracket notation with respect to accessing object properties.