In JavaScript, there are many ways you can use to check whether an object has a specific property or not. In this tutorial, we’re going to see how to do this using the Object.hasOwn() method.

What is the Object.hasOwn() Method

The Object.hasOwn() method is a JavaScript method that can be used to verify whether an object has a property as its own property or not.

The return value for this method can either be true or false. In order for this method to return true, the property you are trying to check has to be directly defined in the object; otherwise, it returns false every time. This is the major advantage of using this method over the Object.hasOwnProperty() method.

Let’s say we have an object like this:

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  isStudent: false,
};

If we want to verify the existence of the firstName property in the person object, we can do the following:

console.log(Object.hasOwn(person, "firstName")); // Output: true

Object.hasOwn() vs Object.hasOwnProperty()

If you’ve looked into it, you’ll find that there’s another way to check if certain properties exist in objects—by using the Object.hasOwnProperty() method. So, what’s the difference?

Basically, the difference is that, the Object.hasOwn() method is better. The Object.hasOwn() method was actually created to replace the Object.hasOwnProperty() method. Here’s why.

Object.hasOwnProperty() Overriding Issue

Whenever the Object.hasOwnProperty() method is overridden (re-implemented), it will no longer be able to accurately determine whether a property belongs to an object. Let’s see an example:

const obj = {
  hasOwnProperty(txt) {
    return txt;
  },
  prop: "That's one small step for man. One giant leap for mankind.",
};

console.log(obj.hasOwnProperty("prop")); // Output: prop
console.log(Object.hasOwn(obj, "prop")); // Output: true

As you can see from the example, the hasOwnProperty() method no longer works in this case, whereas the hasOwn() method still works.

Objects created with Object.create(null)

In JavaScript, nearly everything functions as an object, and each object is connected to a prototype. This prototype, too, is an object, and it possesses its own prototype, which in turn is also an object with its prototype. The point is that, every object in JavaScript is linked within a chain of prototypes. This is known as prototype chain in Javascript.

In the prototype chain, every object has methods and properties. Whenever a property cannot be found directly on an object, Javascript moves up in the prototype chain to find this property until it gets to the end of the chain. This behavior holds true for objects created in Javascript except for the objects created using the Object.create(null) method.

The Object.hasOwnProperty() method of an object in Javascript, is a property that is inherited from an object’s prototype. And whenever you create an object using the Object.create(null) method, the created object is without a prototype and therefore has no properties and no hasOwnProperty() method. Which means that you cannot use the Object.hasOwnProperty() method on objects created using the Object.create(null) method. It simply does not exist.

Let’s see an example:

const foo = Object.create(null);
foo.prop = "exists";
foo.hasOwnProperty("prop"); // Uncaught TypeError: foo.hasOwnProperty is not a function

Running the above example will cause an error. In situations like this, using the Object.hasOwn() method serves as a better alternative. Let’s see what happens when we use the Object.hasOwn() method.

const foo = Object.create(null);
foo.prop = "exists";
console.log(Object.hasOwn(foo, "prop")); // Output: true

As you can guess, we get our desired result.

So the aforementioned reasons are why the Object.hasOwn() method serves as a better alternative to the Object.hasOwnProperty() method.

Note: The Object.hasOwn() property is widely supported but not as well as the Object.hasOwnProperty() method. So do well to check the support of this method before use.