How to check object has Property in JavaScript? [SOLVED]


Written By - Olorunfemi Akinlua
Advertisement

Introduction

In JavaScript, a hash is a data structure that is used to store key-value pairs. A hash is often used to represent a collection of related data, such as a set of user preferences or a list of product options, where each piece of data is identified by a unique key.

 

What is Object?

Think of Object like the primordial object. It’s the object that all objects initially inherit from. And Object implements a few key methods that are a core part of the JavaScript object system. Many of these you won’t use on a daily basis, but there are some methods you’ll see commonly used.

 

Method-1: The hash data structure

The hash data structure is implemented in JavaScript using the "Object" type, which allows you to create objects with properties that are accessed using "dot notation" or "square bracket notation".

Here is an example

let myHash = {};
myHash.key1 = "value1";
myHash["key2"] = "value2";
console.log(myHash);

Output

{ key1: 'value1', key2: 'value2' }

In this example, the myHash object is created using object literal syntax, and two properties are added to the object using dot notation and square bracket notation. The object is then logged to the console, and you can see that it contains the two properties that were added.

 

Method-2: Using the hasOwnProperty method

In addition to using the dot notation and square bracket notation to access the properties of an object (or hashproperty), you can also use the Object.prototype.hasOwnProperty method to check whether an object has a specific property. This method is a built-in method of the "Object" prototype, and it accepts a single argument: the name of the property that you want to check for.

The hasOwnProperty method returns true if the object contains a property having the name . The prototype chain is not examined. This method is useless if the name is hasOwnProperty:

Advertisement
var a = {member: true};
var b = Object.create(a); // from Chapter 3
var t = a.hasOwnProperty('member'); // t is true
var u = b.hasOwnProperty('member'); // u is false
var v = b.member; // v is true

Here is an example of how to use the hasOwnProperty method to check whether an object has a specific property:

let myHash = { key1: "value1", key2: "value2" };
console.log(myHash.hasOwnProperty("key1"));
console.log(myHash.hasOwnProperty("key3"));

Output

true
false

In this example, the hasOwnProperty method is called on the myHash object, and it is passed the name of a property as its argument. If the object has a property with the specified name, the hasOwnProperty method returns true, and if the object does not have a property, it returns false.

We can make use of the hasOwnProperty method alongside other methods such as Object.keys method. In the example below, we use the Object.keys method to get an array of all the keys in an object, and then use the hasOwnProperty method to check if a specific property exists in the object.

const obj = {
    prop1: "value1",
    prop2: "value2",
    prop3: "value3",
};

const keys = Object.keys(obj);

if (obj.hasOwnProperty("prop1")) {
    console.log("obj has a prop1 property");
}

for (const key of keys) {
    if (obj.hasOwnProperty(key)) {
        console.log(`obj has a ${key} property`);
    }
}

Output

obj has a prop1 property
obj has a prop1 property
obj has a prop2 property
obj has a prop3 property

 

Summary

The hasOwnProperty method is a built-in method of the "Object" prototype in JavaScript that is used to check whether an object has a specific property. This method is useful for checking the properties of an object, and it is often used in conjunction with other object methods, such as Object.keys and Object.values, to perform more complex operations on objects.

 

References

Object.prototype.hasOwnProperty() - JavaScript | MDN (mozilla.org)

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment