fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← JavaScript

JavaScript / 2 MIN READ

hasOwnProperty()

Check if objects contain specific properties

From the original Fervor library. Examples may use older package versions.

JavaScript’s .hasOwnProperty() Method: Checking Object Properties

In JavaScript, the .hasOwnProperty() method is a useful tool to determine whether an object possesses a specific property with a given name. This method returns a boolean value indicating whether the property exists directly on the object itself and is not inherited from its prototype chain. In this tutorial, we’ll explore how to use .hasOwnProperty() and its applications.

Introduction to .hasOwnProperty()

The syntax for using the .hasOwnProperty() method is as follows:

object.hasOwnProperty(property);

Where object is the object you want to examine, and property is the name of the property you want to check for.

Using .hasOwnProperty()

Let’s consider an example object named person:

const person = {
    name: "John",
    age: 30,
    address: {
        street: "123 Main St",
        city: "New York",
        state: "NY"
    }
};

You can use .hasOwnProperty() to check if a property exists on the person object:

console.log(person.hasOwnProperty("name")); // true
console.log(person.hasOwnProperty("address")); // true
console.log(person.hasOwnProperty("gender")); // false

In this case, the person object has the properties name and address, so the first two statements return true. However, since the object doesn’t have a gender property, the third statement returns false.

Using .hasOwnProperty() for Looping

One powerful application of .hasOwnProperty() is when looping through an object’s properties and only accessing its own properties, excluding inherited ones. Here’s an example:

for (let prop in person) {
    if (person.hasOwnProperty(prop)) {
        console.log(prop + ": " + person[prop]);
    }
}

In this example, we use a for...in loop to iterate over all the properties of the person object. The .hasOwnProperty() method is employed to ensure that we only access the object’s own properties. This approach helps us avoid handling properties inherited from the object’s prototype chain.

Summary

The .hasOwnProperty() method is a powerful tool for checking whether an object has specific properties directly defined on itself. It is useful in scenarios where you want to distinguish between an object’s own properties and those inherited from prototypes. By using this method, you can create more robust and targeted code for handling object properties.

Remember that the .hasOwnProperty() method doesn’t search up the prototype chain, so it’s suitable for checking an object’s immediate properties.


Feel free to leverage the .hasOwnProperty() method in your JavaScript code to ensure that you’re working with the properties that belong to an object itself, not its prototypes. This method adds clarity and control to your code, improving its reliability and maintainability.

Home


Keep your curiosity going.Explore more JavaScript →
287 TUTORIALS · 22 TOPICSREADY