In your code, myObj.checkProp
(dot notation) looks for a property in your object that’s literally named checkProp
. It fails because myObj
has no checkProp
property, only gift
, pet
, and bed
.
When you use myObj[checkProp]
(bracket notation), checkProp
is substituted with the value contained in the variable checkProp
, so it is evaluated to, say, myObj['gift']
, or myObj['pet']
.
In short, dot notation is useful when you know the name of the property you want to access, and bracket notation for dynamically accessing a property of an object.