Logic behind 'Testing Objects for Properties'

Hi, I’m currently figuring out the logic behind this particular challenge.

I want to know why this code works, meaning it will return a “Not Found” since there’s no somethingElse…

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

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

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




While this doesn’t return “Not Found” and instead shows a blank screen in the terminal.

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

function checkObj(checkProp) {
  
// Your Code Here

  myObj.hasOwnProperty(checkProp);
  
  if (true) {
    return myObj[checkProp];
    
  } else {
  return "Not Found";
  }
}

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

Thanks!

true is always true, so any if statement if (true) will always execute.

1 Like

Huh. I see. That was…pretty obvious now that you’ve pointed it out. :sweat_smile: Thanks a lot!