What Does JavaScript hasOwnProperty() Method Do?

Last updated on: by Digamber
We are going to explore JavaScript’s hasOwnProperty() method in this article. hasOwnProperty() method checks whether the property on an object belongs to the mentioned object or not. If the property belongs to the mentioned object then it will return true else it will return false.

Object’s member inherit the hasOwnProperty() method. This function is used to find out whether the object has the mentioned property directly related to the object.

JavaScript hasOwnProperty() Syntax

obj.hasOwnProperty(property)

property: Pass the string name or symbol to check whether it belongs to the object.

Here the vital thing to be noted down, hasOwnProperty will return true even if you define the undefined or null value.

let a = new Object();
a.propertyOne = null;
a.hasOwnProperty('propertyOne')
// output: true 

a.propertyTwo = undefined;
a.hasOwnProperty('propertyTwo')
// output: true

Using hasOwnProperty() to check if the property belongs to the object exist or not:

var movie = {
  name: 'avenger endgame',
  genre: 'super hit',
}
var song = {
  name: 'kiki do u love me',
}
movie.hasOwnProperty('name');  // returns true
movie.hasOwnProperty('type');   // returns false
song.hasOwnProperty('name');  // returns true
song.hasOwnProperty('status'); // returns false

The main benefit of JavaScript hasOwnProperty() method is it can be initialized with any object by just using the string as an argument. It quickly returns true if the value is available to the object, else it returns false.

function Car(name) {
  this.name = name;
}
Car.prototype.color = 'red';
const bmw = new Car('x1');
console.log(bmw.name); 
// property found on object
console.log(bmw.color); 
// color property found on prototype
console.log(bmw.hasOwnProperty('name')); 
// name is found on the object itself
console.log(bmw.hasOwnProperty('color')); 
// color property is not found on the object itself

In the above example, a new Car object is created. Every Car car is initiate with its own name within the constructor. Although, the color is not mentioned in the object at the beginning. However, available on prototypical hierarchy. So hasOwnProperty() will return true for name but false for color.

hasOwnProperty() works very smoothly when it comes to loop through an object. Now you can easily find out if the properties of the object belong to the object, not from the prototype.

// declaring a Car function
function Car(name) {
  this.name = name;
}
// setting up new prop with protoype 
Car.prototype.color = 'red';
// creating a new Car object
const BMW = new Car('x1');
// looping through every car prop including prototype as well
for (let car in BMW) {
  car + ':', BMW[car];
}
/* 
   outupt: name: x1
   outupt: color: red
*/

/**************************************/
/*will loop through only self properties of the object, 
excludes property generated through prototype method */
for (let car in BMW) {
    if(BMW.hasOwnProperty(car)){
      console.log(car + ':', BMW[car]);
    } 
}
// output: name:

hasOwnProperty() Browser Support

ChromeFirefoxEdgeSafariOpera
YesYesYesYesYes

Browser compatibility source MDN Web Docs

positronX.io - Tamso Ma Jyotirgamaya
Digamber

A Full-stack developer with a passion to solve real world problems through functional programming.