Every object in JavaScript has its own specific set of properties which you can add, change or remove. In order to check whether the object contains a certain property or not you can use the hasOwnProperty() method.

Since inheritance is possible in JS, every object contains a reference to its prototype. If you are trying to access a certain property, first of all, the program looks for it in the object itself, then in its prototype, after that in the prototype of this prototype and so on, until the property with the specified name is found or the chain of prototypes is finished.

If you need to find out if the property belongs to the object itself and not its prototype, the hasOwnProperty() method also can help you with that. Unlike the in operator, it does not check the existence of properties in the prototype chain.

Notes on the hasOwnProperty()

In order to call this method, you need to use the variable containing the object and pass the name of the property as a parameter (it’s obligatory). The hasOwnProperty() method returns a boolean value true if the object has the property with the specified name and it’s not inherited. In case there are no properties matching the name you have passed as an argument, or this property is inherited from the object’s prototype, the method will return false.

The following code illustrates the use of the hasOwnProperty() method.

const person = {
 name: "John",
 age: 25,
};
console.log(person.hasOwnProperty("age")); // logs true
console.log(person.hasOwnProperty("status")); // logs false

You can call this method even using literals of primitive data types such as numbers, strings or boolean values. In this case they will be automatically converted to the instances of the wrapper objects: Number, String or Boolean respectively.

In order to call the hasOwnProperty() method using a literal you just need to surround it with parentheses. For example, checking if there is a length property in a string literal will return true since this property belongs to a String wrapper object.

console.log(("A test string").hasOwnProperty("length")); // logs true

At the same time, using a numeric literal to check if it contains, for example, MIN_VALUE will return false because this property is inherited from the Number.prototype and does not belong to the Number itself. The same situation is with a Boolean wrapper object. It does not have its own properties, so the hasOwnProperty() method will always return false.

console.log((1).hasOwnProperty("MIN_VALUE")); // logs false

Such primitives as null and undefined will not be converted into any wrapper objects and will cause the Type Error message if you try to use them with the hasOwnProperty() method. The same error will happen if you call this method using the object created with the help of Object.create(null) because it’s not inherited from the Object.prototype and thus does not have access to this method at all.

At the same time, if you use the proper object for calling this method and the value of the existing property is equal to null or undefined, this method will still return true.

Examples

Quite often programmers may face a need to loop through an object’s properties. They usually do that with the help of the in operator. When you need to filter all the properties and use only non-inherited ones, the hasOwnProperty() method is perfectly suitable.

Let’s create two objects (e.g. food and drinks) with two properties each and make one of them a prototype to another.

const food = {
 meat: 1,
 rice: 2
};
const drinks = {
 water: 3,
 juice: 4
}
drinks.__proto__ = food;

Using the in operator you can easily go through all the properties of the drinks object and print them to the console. However, since it has a prototype, not only “water” and “juice” will be printed but also every property of the food object.

By adding the hasOwnProperty() method to the simple if clause you can separate the properties and perform different actions for inherited and non-inherited ones. The following code illustrates such an implementation.

for (const item in drinks) {
 if(item.hasOwnProperty)
 console.log("The own property: " + item); // logs "water juice"
 console.log("Inherited property: " + item); // logs "meat rice"}

This way, besides the regular check if the object contains a certain property, this method can also help you to filter the inherited ones. Note that the name of this method is not reserved in JavaScript which means that you are allowed to create your own property with such a name.

The output after running the code below might be a bit confusing. There is an object (“person”) containing two properties: “name” and “hasOwnProperty”, which is a function returning false every time. In this case if you call the hasOwnProperty ('name') method, the result would always be false, even though the object does contain the specified property.

const person = { name: "John",
 hasOwnProperty: function() {return false;}
};
console.log(person.hasOwnProperty('name')); // logs false

In order to avoid such confusing results, in this situation you can use the hasOwnProperty() method of another object and call it passing the current object as an argument. Or you can use the hasOwnProperty()from the Object.prototype (in this case the new object is not created). These two ways are shown in the example below.

console.log(({}).hasOwnProperty.call(person, 'name')); // logs true
console.log(Object.prototype.hasOwnProperty.call(person, "name")); // logs true