JS Built-ins
ObjectObject.getOwnPropertySymbolsbeginner

Object.getOwnPropertySymbols() β€” Object.keys misses 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() β€” invisible to Object.keys

Symbol properties are completely invisible to Object.keys, Object.values, and Object.entries.

Challenge: Implement onlyStringKeyCount(obj) that returns the number of string keys using Object.keys, demonstrating that Symbols are not counted.

const s = Symbol('x')
onlyStringKeyCount({ a: 1, [s]: 2 }) // β†’ 1 (symbol not counted)
πŸ’‘ Hints (1)
  • β€’ `Object.keys` only sees string-keyed enumerable properties. Use `Object.getOwnPropertySymbols` for Symbol keys.