Dot or braket notation?

Hey! I don’t know if that has been asked before.
I was doing this exercise:

and my solution was exactly the same as camperbot’s

except that I used dot.notation instead of braket[notation] on the third line. So I typed:

return obj.checkProp;

instead of:

return obj[checkProp];

and my solution was not accepted. I went over my code many times and I tried brakets out of curiosity and it worked. Why is that? Thank you!

2 Likes

Hello~!

Dot notation looks for a specific property called “checkProp”, where bracket notation looks for a property called the value of the “checkProp” variable.

7 Likes

If, I only want using dot notation, instead of bracket notation, is there a way to do that?

you need to not use variables

It took me five minutes to figure out that obj.checkProp would return undefined as it was looking for the the value “checkProp” in obj that does not exist.
return obj[checkProp]; uses the value stored inside checkProp so it would actually look for obj.gift and return pony etc.

i find console.log(variable in here); very useful to find the issues in my code :slight_smile: