Need HELP in how to access an object property!

Tell us what’s happening:
As u guyz can see in the source code, the part:

                                           if (obj.hasOwnProperty(checkProp)) {
                                                 return ***obj[checkProp];***
                                              } 

I tried it and had been checked that that is corect, but when I changed it to
obj.checkProp;
it couldn’t work, it was incorect.
I thought that we have couples of way to accessing an object property and all of this is worked. Can anyone explain to me what is the problem?
Many thanks.
Your code so far


function checkObj(obj, checkProp) {
// Only change code below this line
if (obj.hasOwnProperty(checkProp)) {
  return obj[checkProp];
}
return "Not Found";
// Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

That’s because checkProp is a variable. In such case you can use only brackets.

However if you are using the actual property name then you can use dot notation as well.

var myObj = {
  top: "hat",
  bottom: "pants"
};
console.log(myObj.top) // hat
console.log(myObj['top']) // hat
1 Like

oh I see it. Thank you.