JS Built-ins
ObjectObject.hasOwnintermediate

Object.hasOwn() β€” works on null-prototype objects

πŸ“– 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() β€” null prototype objects

obj.hasOwnProperty throws on null-prototype objects because they don't inherit it. Object.hasOwn works safely on them.

Challenge: Implement checkNullProto(key, val) that creates a null-prototype object with the given key/val, then uses Object.hasOwn to check for that key.

checkNullProto('id', 42) // β†’ true
πŸ’‘ Hints (1)
  • β€’ `Object.hasOwn` is a static method and never relies on the object's own `hasOwnProperty`.