JS Built-ins
ObjectObject.hasOwnintermediate

Object.hasOwn() β€” vs hasOwnProperty

πŸ“– Usage Example
// Check if a property is directly on the object
const obj = { name: 'Alice' }
Object.hasOwn(obj, 'name')      // β†’ true
Object.hasOwn(obj, 'toString')  // β†’ false

Use Object.hasOwn() to reliably check if a property is an own property, without being fooled by inherited properties.

Object.hasOwn() β€” safer than hasOwnProperty

Object.prototype.hasOwnProperty can fail if the object has a property named hasOwnProperty or has no prototype. Object.hasOwn is always safe.

Challenge: Implement safeCheck(obj, key) that uses Object.hasOwn (not hasOwnProperty) to check ownership.

safeCheck({ a: 1 }, 'a') // β†’ true
πŸ’‘ Hints (1)
  • β€’ `Object.hasOwn` works even on objects with no prototype (`Object.create(null)`) or with a shadowed `hasOwnProperty`.