JS Built-ins
ObjectObject.getOwnPropertySymbolsbeginner

Object.getOwnPropertySymbols() β€” multiple symbols

πŸ“– Usage Example
// Get all Symbol-keyed properties
const sym = Symbol('id')
const obj = { [sym]: 1, name: 'Alice' }
Object.getOwnPropertySymbols(obj)   // β†’ [Symbol(id)]

Use Object.getOwnPropertySymbols() to retrieve all symbol-keyed own properties, which are invisible to Object.keys().

Object.getOwnPropertySymbols() β€” counting multiple symbols

An object can have multiple Symbol-keyed properties. Object.getOwnPropertySymbols returns all of them.

Challenge: Implement countSymbols(obj) that returns the number of Symbol-keyed properties.

const s1 = Symbol('a'); const s2 = Symbol('b')
countSymbols({ [s1]: 1, [s2]: 2 }) // β†’ 2
πŸ’‘ Hints (1)
  • β€’ `Object.getOwnPropertySymbols(obj).length` gives the count of Symbol properties.