Is there a situation where a dot notation would work and not bracket notation?

During the exercise, it appears the return obj.checkProp; would not work because brackets are required to access a variable.

If this is the case, then is there an opposite situation where a dot annotation would be required and bracket would not work?

I’m new to all of this, so if bracket is more useful in this way I could just stick to using that. Initially I thought they were the same.


Your code so far

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

Your browser information:

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

Challenge: Testing Objects for Properties

Link to the challenge:

No, but prefer dot notation where possible because it’s easier to read and easier to type (“I am accessing a property with the key of x on object y”). And bracket notation for when it’s necessary, ie if you need to dynamically look up a property on an object based in some other logic that you need to evaluate first

That’s actually what I was thinking, but since the bracket is effective in more situations I may sacrifice it’s aesthetics.

It’s not about aesthetics, it’s about clarity. Dot notation is immediately readable, bracket notation not as much.

You will likely also prefer it from an IDE/Editor point of view as the code completion (IntelliSense) is faster with dot notation.

1 Like