Basic JavaScript - Testing Objects for Properties

Tell us what’s happening:

Why is the console returning undefined? i used an if statement. If the value of obj.hasOwnProperty(checkProp) equals true, then return the value of property of object, else return the string “Not Found”. Problem is that in the case obj.hasOwnProperty(checkProp) is false, it’s returning “Not Found”, but in the case true, it’s returning undefined, not the value of obj.checkProp. Can anyone please tell where i am getting wrong?

Your code so far

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

console.log(checkObj({gift: "pony", pet: "kitten", bed: "sleigh"}, "gift"));

Screenshot 2024-05-28 112656

Your browser information:

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

Challenge Information:

Basic JavaScript - Testing Objects for Properties

Welcome to the forum @rockstarankit00

You using a string to access obj

Happy coding

1 Like

But this:

return obj.checkProp;

is also returning undefined, and

return obj["checkProp"] ;

is also returning undefined, tried both dot notation and bracket notion to acccess checkProp property of obj, both are returning undefined :slightly_frowning_face:

Try using bracket notation with the variable name, not the string containing it.

1 Like

yes solved now, thankyou :blush:

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