When to use braket or dot notation to access

Tell us what’s happening:
I just completed this challenge by returning obj[checkprop], but I wanted to check what happened if I returned obj.checkprop, which did not work. Why does it not work using dot notation is this scenario?

Thanks for any feedback!

Your code so far


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36.

Challenge: Testing Objects for Properties

Link to the challenge:

Welcome, sanderheleren.

This is a common misunderstanding, but these lessons explain the difference:

If those do not help, others have explained accessing object properties quite well, in other forum posts. So, I suggest you search there.

Hope this helps

2 Likes