JS Built-ins
ObjectObject.getOwnPropertyNamesintermediate

Object.getOwnPropertyNames() β€” array-like includes length

πŸ“– Usage Example
// Get all own property names, including non-enumerable
const obj = { a: 1 }
Object.defineProperty(obj, 'hidden', { value: 2, enumerable: false })
Object.getOwnPropertyNames(obj)   // β†’ ['a', 'hidden']

Use Object.getOwnPropertyNames() to list all own properties including non-enumerable ones, unlike Object.keys().

Object.getOwnPropertyNames() on arrays

Arrays have a non-enumerable length property. Object.getOwnPropertyNames includes it, while Object.keys does not.

Challenge: Implement namesIncludeLength(arr) that returns whether Object.getOwnPropertyNames of arr includes 'length'.

namesIncludeLength([1, 2, 3]) // β†’ true
πŸ’‘ Hints (2)
  • β€’ `Array.prototype.length` is a non-enumerable own property of every array.
  • β€’ It appears in `getOwnPropertyNames` but not in `Object.keys`.