JS Built-ins
ObjectObject.hasOwnintermediate

Object.hasOwn() β€” inherited property returns false

πŸ“– 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() β€” own vs inherited

The in operator finds inherited properties, but Object.hasOwn does not β€” it is strictly limited to own properties.

Challenge: Implement ownVsInherited(proto, obj, key) that returns an object with ownResult (from Object.hasOwn) and inResult (from key in obj).

const proto = { x: 1 }
const obj = Object.create(proto)
ownVsInherited(proto, obj, 'x')
// β†’ { ownResult: false, inResult: true }
πŸ’‘ Hints (1)
  • β€’ `in` traverses the prototype chain; `Object.hasOwn` does not.