Bracket access [ ]
With `obj[key]` you read a property using an expression, like a string or a variable, instead of a hand-written name.
Brackets read a property from an object (or an item from an array) using the value inside them.
product["priceInCents"] does the same as product.priceInCents. The difference is that inside the brackets goes an expression: a string, a variable, or a calculation. With the dot, the name is fixed in the code.
That is why brackets help when the key arrives in a variable, or has characters the dot can't take, like a hyphen.
On arrays, the number is the position, and it starts at 0.
const key = "name";
console.log(user[key]);
In lesson 01.03 the course uses product["priceInCents"] to show the key as a string. TypeScript checks that the key exists in the type; when Node runs it, only the access remains, with no types.
Reach for it when: the key is in a variable, when it isn't a valid name for the dot, or when you read an array item by its position.
Examples
Common mistake
const product = { name: "Pen", priceInCents: 150 };
const key = "name";
console.log(product.key);Why: The dot looks for a property literally named `key`; it doesn't use the variable's value.
How to write it: Use brackets: product[key].
