// defining properties
{
age : 26, // normal string property name
1 : '1 is OK', // normal string property name
const: 'reserved word is OK', // reserved word as property name
"full name": "John Doe", // string as property name
}
// accessing properties
obj . prop // by identifier
obj [ prop ] // by String / Symbol.
// a symbol
let symbol = Symbol('hi');
// an object
let obj = {
// string as property name
name : 'normal property names are converted to strings ("name")',
const : '⭐ reserved word as property name is OK',
1 : '⭐ 1 is legal property name, coerced to "1".',
'full name' : 'string as property name',
symbol : 'this `symbol` is a normal string property name ("symbol")',
// symbol as property name
[symbol] : 'symbol as property name', // computed property name
};
obj['full name'], // 'string as property name'
obj[symbol], // 'symbol as property name'
obj.symbol, // 'this `symbol` is a normal string property name ("symbol")'
obj.const, // '⭐ reserved word as property name is OK'
obj[1], // ✅ "bracket notation" is always safe.
// obj.1, // ❗ "dot notation" requires an identifier
// ^^ // ⛔ SyntaxError: Unexpected number
1 in obj, // true (1 -> '1')
'1' in obj, // true (normal string property name)
'name' in obj, // true (normal string property name)
symbol in obj, // true (symbol as property name)
'symbol' in obj, // true (normal string property name)