Check if an Object has a Property in JavaScript
There are a few trusted methods to know whether the object has a specific property or not when working with Javascript. This blog will take you through two of the best methods to identify whether an object has property or not. We’ll start with discussing the hasOwnProperty method in javascript and then proceed to discuss the ‘in’ method. We’ll also throw some light on how these methods differ from each other, so read along to know more.
hasOwnProperty method
The hasOwnProperty()
Is a property that returns true only if the specific property is a direct property of the object in consideration, it doesn’t matter if the value is undefined or even null. hasOwnProperty()
Only returns false when either the property hasn’t been declared at all or is inherited. This method has widespread usage as it can be used with almost all Javascript objects.
hasOwnProperty has its own share of advantages and disadvantages and one of the best advantages of this method is that it is so easy to initialize with any object! Users can simply use the string as an argument to do so, it will quickly return true or false depending on whether the value is available to the object or not.
This method is used to check whether the property of 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.
hasOwnProperty has pretty simple syntax i.e. object.hasOwnProperty(propertyname)
Now to understand how to pass an argument using hasOwnProperty()
, Let’s take you through the process with some examples:
Now, to begin with, our aim is to pass the string’s name and check whether it belongs to the object or not. So, every object in Javascript returns a boolean value when object.hasOwnProperty('prop')
Is used. This indicates that the object has a property prop
.
Example
Now, let us consider an example code where hasOwnproperty is used to determine whether the property ‘age’ and ‘name’ are present or not:
const actor = {
name: 'Leanardo'
};
actor.hasOwnProperty('age'); // => false
actor.hasOwnProperty('name'); // => true
actor.hasOwnProperty('name')
Is seen to return true because the property name evidently exists in the object actor while on the other hand, our object, actor, doesn’t have any property named age and hence results in false, which denotes an absent property.
hasOwnProperty is found to look for properties that are inherent i.e. The object’s own properties that are linked directly to them. Due to this reason, hasOwnProperty()
doesn’t detect anything that is not a direct property or is an inherited property, which means that this cannot detect the toString
property. So, if there’s an inherited property then the hasOwnproperty returns false.
One disadvantage that you may come across while using the hasOwnproperty method is that hasOwnProperty will be left useless if the object is found to define a property with the same name hasOwnProperty.
‘in’ operator method
When using the ‘in’ operator, it returns a true value only if the specified property is present in the object and can be used versatilely for objects as well as arrays. Let us show you an example where in operator is being used to detect whether name and age are properties in the object actor.
const actor = {
name: 'Leonardo'
};
'name' in actor; // => true
'age' in actor; // => false
‘name’ being a property above comes out to be true and age doesn’t. in operator follows a short and simple syntax and many people prefer it over hasOwnproperty method due to its ease of use.
Now, the basic difference between both the methods discussed above i.e. in an hasOwnproperty method, is that the in operator checks within the object for both object’s own as well as inherited properties while hasOwnproperty looks for only the object’s own properties.
Difference between hasOwnProperty and ‘in’ Operator
hasOwnproperty and in command, both have many similarities and have a similar objective i.e. to check if a specified object contains a mentioned property or not. As discussed above as well, the key difference between both these methods is their ability to distinguish between inherited and specific properties. This is because unlike the in method, hasOwnproperty method doesn’t follow the same procedure and doesn’t look for the properties in the prototype chain of the object.
Also, most users prefer hasOwnProperty() over the in method as hasOwnProperty allows you to avoid issues with some keys like constructors. Another drawback in both these methods is rather a significant one, both of them are designed to identify whether the given property belongs to the object or not, but cannot confirm whether these properties have any significant or real values to them or not.
To Summarize
To conclude, most users prefer to use hasOwnproperty in cases where you need to check whether an object has a property or not, but in cases where you need to check for a function in an object, like checking for tostring(), they prefer using in method then.
Apart from the two methods discussed above, one can also use a simple approach of just calling object.propName !== undefined
and simply compare it next to the undefined directly.
Sharing is caring
Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: