Why property isn't accessible using dot?

I was stripping out the code to make it run and encountered a question when I want to access the property with a . (dot) it gives me undefined but when I use [checkProp], it prints out the correct value. why?

 function checkObj(obj, checkProp) {
  // Only change code below this line
  //return "Change Me!";
    if(obj.hasOwnProperty(checkProp)){
      console.log(obj[checkProp])
    console.log(obj[checkProp]) //returns undefined
    }

  // Only change code above this line
}

checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift") 
     

function checkObj(obj, checkProp) {
// Only change code below this line
//return "Change Me!";
for(let prop in obj){
  if(obj[prop].hasOwnProperty(checkProp)){
    console.log(obj[prop].checkProp)
  }else{
    return "Not Found"
  }
}
// Only change code above this line
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

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