JS Built-ins
← Object121 / 150
ObjecthasOwnPropertybeginner

hasOwnProperty() β€” basics

πŸ“– 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.

Object.prototype.hasOwnProperty()

obj.hasOwnProperty(key) returns true if the object has the specified key as its own (not inherited) property.

Challenge: Implement isOwnProperty(obj, key) that returns whether the key is an own property of the object.

πŸ’‘ Hints (1)
  • β€’ hasOwnProperty does not traverse the prototype chain