JS Built-ins
ObjectObject.hasOwnbeginner

Object.hasOwn() β€” own property returns true

πŸ“– 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() (ES2022)

Object.hasOwn(obj, key) returns true if obj has an own property named key. It is a safer replacement for Object.prototype.hasOwnProperty.call.

Challenge: Implement checkOwn(obj, key) that uses Object.hasOwn to check for an own property.

checkOwn({ a: 1 }, 'a') // β†’ true
checkOwn({ a: 1 }, 'b') // β†’ false
πŸ’‘ Hints (1)
  • β€’ `Object.hasOwn(obj, key)` is equivalent to `Object.prototype.hasOwnProperty.call(obj, key)` but cleaner.