JS Built-ins
← Object125 / 150
ObjecthasOwnPropertyintermediate

hasOwnProperty() β€” safe null-prototype usage

πŸ“– Usage Example
// Check for own (non-inherited) property
const obj = { a: 1 }
obj.hasOwnProperty('a')          // β†’ true
obj.hasOwnProperty('toString')   // β†’ false

Use hasOwnProperty() to check whether a property exists directly on an object rather than on its prototype chain.

hasOwnProperty() β€” safe usage pattern

Objects with a null prototype (e.g., from Object.create(null)) don't have hasOwnProperty. Use Object.prototype.hasOwnProperty.call(obj, key) as the safe pattern.

Challenge: Implement safeHasOwnProperty(obj, key) using the safe call pattern.

πŸ’‘ Hints (1)
  • β€’ Use Object.hasOwn() (ES2022+) or Object.prototype.hasOwnProperty.call() for safety