Testing Objects for Properties - Bracket vs Dot Notation

Testing Objects for Properties - Bracket vs Dot Notation
0.0 0


Hey guys,

I was only able to pass the Testing Objects for Properties using bracket notation - the exact same code would not work if I used dot notation. Can anyone tell me why that’s the case? I’m really curious and I haven’t been able to find any answers so far!

Thanks :slight_smile:

Link to the challenge:


That’s how it works, you need bracket notation when using a variable instead of a property name. The dot notation is only for the actual property name.

Bracket notation is useful if you have a property name that has special characters in it, like obj[“hello world!”] – such properties are often referred to as keys when accessed via bracket notation. The [ ] notation requires either a variable (explained next) or a string literal (which needs to be wrapped in " … " or ’ … ').
Of course, bracket notation is also useful if you want to access a property/key but the name is stored in another variable.

From: https://github.com/getify/You-Dont-Know-JS/blob/master/up%20&%20going/ch2.md