Dot versus Bracket Notation / Objects

Hello,

I understand this exercise except for one thing. The ** part in the code below is why the solution isn’t accepted. Instead, it’s supposed to be in bracket notation:

return obj[checkProp];

Can we not use dot notation for this? I prefer it, but this is more of a conceptual question. Thanks!!!





function checkObj(obj, checkProp) {
if (obj.hasOwnProperty(checkProp)) {
  **return obj.checkProp**;
} else {
  return 'Not Found';
}
}

Challenge: Testing Objects for Properties

Link to the challenge:

obj.checkProp will return undefined unless the object named obj has a property with the actual name “checkProp”.

That isn’t the case. checkProp is a variable.

2 Likes

Hey @CAL90,

Yeah, you can’t use dot notation like this as what you’re doing is essentially getting the property checkProp from obj. You cannot dynamically access an object’s property using dot notation. This is why the bracket notation exist, so you can dynamically access an object’s property

2 Likes