Testing Objects for Properties13

Tell us what’s happening:

Well, checkProp returns “gift”, myObj.gift returns “pony” and myObj.checkProp returns nothing. Why is it so, can anybody help?

Your code so far

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

function checkObj(checkProp) {
  if (myObj.hasOwnProperty(checkProp)===true) {
  } else {
    console.log ('Not found');

// Test your code by modifying these values

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 OPR/52.0.2871.40.

Link to the challenge:

Hey @4Nancy,
You don’t have to console.log() your result.
You will need to return your result.

Same goes for this one as well.

1 Like

Yeah, I know.
But console.log(myObj[checkProp]); works for some reason and console.log(myObj.checkProp); doesn’t

The above is undefined, because there is no actual property named “checkProp” in myObj. When you use dot notation with an object, the actual property name you specify after the dot must exist in the object.

You might want to review the following challenges on accessing object properties to make sure you understand when you can use dot notation and when you must use bracket notation. Basically, you can always use bracket notation, but depending on the circumstance, you might be able to use dot notation instead.





Now I see.

Thank you!

Why do we have to use the quotes here, even if the object properties contain no space?
if I write this code:


I get:
gift is not defined

Thank you

Because if you write gift then it is considered a variable and is evaluated as such (as you have not assigned anything to gift its value is undefined, or it throw an error because it has not been declared anywhere), instead "gift" is a string and it is treated as it is

1 Like

Thank you. Actually, I’ve never thought about properties as variables before…I wonder if there is a hidden code interpreted by the program while creating an object with a property’s key called: gift, like this:

var myObj = {
  //var myProp = "gift";
  gift: "pony",