JavaScript's hasOwnProperty() Method

Image by Anne Nygård.

In Brief

hasOwnProperty() checks whether a property belongs directly to an object rather than being inherited through its prototype chain. In modern JavaScript, prefer Object.hasOwn() for this check because it also works safely with nullprototype objects and objects that shadow hasOwnProperty. Use the in operator instead when inherited properties should count as well.

In JavaScript, Object.prototype.hasOwnProperty() is a relatively simple method for object manipulation and introspection. Here I'd like to quickly explain hasOwnProperty(), its purpose, and offer a practical example to illustrate its use.


Understanding hasOwnProperty()

hasOwnProperty() is a method used to check whether an object has a property as its own (not inherited from its prototype chain).

Syntax

obj.hasOwnProperty(prop)
  • obj: The object that we want to test.
  • prop: The property name (string) to check for.

Why Use hasOwnProperty()

In JavaScript, objects can often inherit properties from their prototype. hasOwnProperty() helps in determining if a property is actually part of the object itself, rather than inherited, in which case you may want to process it differently.


An Example in Code

Consider an object person with its own properties:

const person = {  name: 'Lola',  age: 30,};console.log(person.hasOwnProperty('name'));  //=> trueconsole.log(person.hasOwnProperty('toString'));  //=> false
  • 'name' is the object's own property, so hasOwnProperty('name') returns true.
  • 'toString', however, is inherited from the object's prototype, so hasOwnProperty('toString') returns false.

Object.hasOwn() vs. hasOwnProperty()

hasOwnProperty() is useful to understand, but for new code I would usually reach for Object.hasOwn(object, property) first. It avoids calling a method from the object being checked, which is safer when the object has a strange prototype or a property named hasOwnProperty.

That is not just theoretical. Objects created with Object.create(null) do not inherit hasOwnProperty(), and usercontrolled data can sometimes shadow names you expected to be safe.

There is also an important distinction between these checks and JavaScript's in operator. Both Object.hasOwn() and hasOwnProperty() test whether a property belongs directly to the object. in, on the other hand, also returns true when the property exists somewhere in the object's prototype chain.

const person = {  name: 'Lola',};Object.hasOwn(person, 'name');      //=> trueObject.hasOwn(person, 'toString');  //=> false'name' in person;                   //=> true'toString' in person;               //=> true

Neither behaviour is inherently wrong; they answer different questions. Use in when inherited properties should count, and Object.hasOwn() when you specifically need to know whether the object itself defines the property.

For modern code, that makes Object.hasOwn() the clearest general replacement for the hasOwnProperty() checks shown earlier in this article.


Wrapping Up

Object.prototype.hasOwnProperty() remains useful to understand because it explains an important distinction in JavaScript between an object's own properties and those inherited through its prototype chain. In modern JavaScript, Object.hasOwn() is generally the better choice when making that check directly, whilst the in operator remains useful when inherited properties should count as well.

Key Takeaways

hasOwnProperty() and Object.hasOwn() check whether a property belongs directly to an object, whilst the in operator also considers properties inherited through the prototype chain. For new code, prefer Object.hasOwn() when you specifically need an ownproperty check.


Need a senior engineer involved?

I can work directly in the codebase, review the architecture, or support the team through delivery when the work needs more than extra hands.