➕bracket notation []
`obj[prop]` syntax.
// ⭐️ bracket notation
obj [ prop ] // obj: non-nullish, prop: string | symbol (any other value coerced to string)// object
let obj = { name: 'Joe' };
// ⭐️ bracket notaton
// -------------------------------------------------------------------
// • syntax: `obj [ prop ]`
// • obj : expression (❗ value of null/undefined raises TypeError)
// • prop: expression (❗ evaluated and "converted" to string)
// -------------------------------------------------------------------
obj[''] = 'joe'; // ✅ "empty string" is OK
obj[' '] = 'OK'; // ✅ "space" is OK
obj[true] = true; // ✅ boolean is OK
const key = "name"; // variable
const getKey = () => "name"; // function
// ⭐️ `prop` can be any expression.
obj["name"], // 'Joe' (prop: string literal)
obj[key], // 'Joe' (prop: variable)
obj[getKey()], // 'Joe' (prop: function return value)Last updated