In this article, we will be taking a deep dive into the hasOwnProperty() method that helps in checking whether a property exists within an object. Let us get started!
What is the hasOwnProperty() Method in JavaScript?
We will begin with a short introduction of Objects. Objects are non-primitive data types in Javascript that store an unordered collection of key-value pairs. The association between the key and value is called property, so objects are a collection of properties.
For example, we have defined an object “student” with various properties below:
let student={
name:'Komal',
rollNumber:200101,
age:21
}
Several properties such as name, rollNumber, and age are defined for the object student”. The name, rollNumber, and age here indicate the keys, and “Komal”, 200101 and 21 are the values corresponding to the related keys.
The hasOwnProperty() is an in-built method in JavaScript is used to check if an object contains a property. This method takes an argument in the string form as the parameter and checks whether that string (the property) is present in the object or not.
It returns a boolean value. If it returns true, it means that the property exists in the object. If it returns false, it means that the object doesn’t contain that particular property.
The syntax for using this method is as follows:
object_name.hasOwnProperty(‘property_name)
Let us understand it with the help of the previous example. We have an object ‘employee’ with certain properties(key-value pairs). Now, We will check if the object employee contains a certain property or not using this method. The code for the same is given below:
//defining an object employee let employee={ empId:2301, name: "Joshua", email:"[email protected]", age:32 }; //both the statements below return true console.log(employee.hasOwnProperty('name')); console.log(employee.hasOwnProperty('age')); //below statement returns false console.log(employee.hasOwnProperty('salary'));
Output:
true
true
false
The code above uses the inbuilt method ‘hasOwnProperty(‘property_name’)’ and returns true for the properties – name and age. It returns false when checking for “salary”.
When working with objects in JavaScript, it’s important to distinguish between properties that are directly defined on the object and those inherited from its prototype. Using hasOwnProperty allows developers to ensure they are only considering properties that belong directly to the object.
The hasOwnProperty() method also considers properties with values of null or undefined as direct properties of the object. Let us see an example of the sample:
//defining an object employee let employee={ empId: null, name: undefined, }; //both the statements below return true console.log(employee.hasOwnProperty("empId")); console.log(employee.hasOwnProperty("name"));
In this example, we have an object employee with properties empId and name, both having values of null and undefined, respectively. Themethod returns true for both properties, indicating that they are direct properties of the object.
hasOwnProperty() vs the “in” Operator
While the hasOwnProperty() method is effective for checking the direct properties of an object, the “in” operator is used to determine if a property exists in an object, regardless of whether it is a direct property or an inherited property.
The syntax of the in operator is:
“propertyName” in objectName;
where,
propertyName: name of the property to check
objectName: name of the object in which property has to be checked
Let us see an example of the same:
//defining an object employee let employee={ empId:2301, name: "Joshua", email:"[email protected]", age:32 }; //both the statements below return true 'name' in employee; 'age' in employee; //below statement returns false 'salary' in employee
Output:
true
true
false
The above code checks if the name and age properties exist in the object ‘employee’ and hence returns true, whereas returns false when checking for the property ‘salary’ as it doesn’t exist in the object.
It’s important to choose the appropriate method based on the specific use case.
- If we want to check for direct properties only, use the hasOwnProperty() method.
- If we need to check for properties in both direct and inherited ways, the “in” operator is the preferred choice.
Can hasOwnProperty be used with any type of object? Yes, this method available on all objects in JavaScript. It can be used with objects created using object literals, constructor functions, or instances of built-in classes.
However, one of the common limitations when iterating over object properties, leading to unintended inclusion of inherited properties. It’s essential to keep this in mind to ensure accurate and expected behavior when working with objects.
Conclusion
In this article, we discussed the objects and the hasOwnProperty() method to check if a property exists within an object. Then we compared the hasOwnProperty() method and the “in” operator. Both methods are used to check the existence of a property within an object but have certain differences.