JS Built-ins
← Object122 / 150
ObjecthasOwnPropertybeginner

hasOwnProperty() β€” vs inherited

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

The key difference between hasOwnProperty() and the in operator is that in checks the entire prototype chain, while hasOwnProperty() only checks the object itself.

Challenge: Show the difference between in and hasOwnProperty().

πŸ’‘ Hints (1)
  • β€’ hasOwnProperty checks only own props; in checks the full prototype chain