Brackets vs Dot Notation

On Basic Data Structures: Access Property Names with Bracket Notation, why does:

return foods[scannedItem];

work correctly but:

return foods.scannedItem;

not work?

Hi @76tuz

Because bracket notation computes the value passed in to the brackets before accessing the value on the object.

So in that challenge, "apples" is passed into the checkInventory function so doing foods[scannedItem] is the same as doing foods.apples.

By doing foods.scannedItem, you’re basically asking for a property named scannedItem on the object which in this case doesn’t exist.


Thanks! That makes sense… the dot notation means you’re accessing an actual object property, which in this case, doesn’t exist in the foods object.