Why can't I use checkProp instead of obj[cehckProp]

For the first if statement if I replace obj[checkProp] with just checkProp it gives me an error. I don’t understand why. Can someone please help me?

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
}

  **Your browser information:**

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

Challenge: Testing Objects for Properties

Link to the challenge:

How else do you access an object’s property then?

For instance, here’s an object:

const dog = {
  name: "Cotchi"
}

You can’t access the name property by just using “name”.

You need to have this format: dog[name], there, you’re accessing the name.

In the challenge, you’re checking if a certain property exists on an object. So how else would you check it without accessing the properties first using bracket notation?


Also, bracket notation is used when using function parameters as the value of properties. Dot notation is if you’re accessing an specific property. You can access the example above using dot notation: dog.name

If you replace the variables with numbers wouldn’t my code be returning the value of checkProp. I’m still a little confused.

checkProp is a string, it’s possibly one of the keys of the object obj. Remember that objects consist of a group of key-value pairs. For this challenge, you need to return the value that corresponds to the key represented by checkProp.

Ah I understand now. Thanks!

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