Just like we use hashmaps in C++ that store data in the form of “key-value” pairs, We use Objects in JS to store data in the same way, i.e. in the form of key-value pairs. In this article, we will be taking a deep dive into what Javascript objects are and how to check if a property exists in an object. We will discuss various methods to check for key existence in an object.
3 Ways to Check if Property exists in Object
The association between key and value in an object is called property, so objects are a collection of properties. When working with objects, we might need to check if the property exists before attempting to access its value. Also, this allows you to implement conditional logic based on whether a specific property is present or not.
Let us discuss the various methods to check if the JavaScript object has a particular property:
1) Using the ‘in’ Operator
A simple way to check if the object contains a certain property is by using the “in” operator. It returns true if the property is found, and false otherwise. 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.
2) Using the hasOwnProperty() Method
The best wa to check if a JavaScript object contains a property is by using an inbuilt ‘hasOwnProperty()’ method. 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”.
3) Using the typeof Operator
The typeof operator is used to determine the type of a value in JavaScript. It can also be used to check if a property exists in an object by comparing the property to undefined. If the type of the property comes out to be undefined, then the property does not exist in the object.
The basic syntax for using the typeof operator:
typeof(object_name.property_name)
where,
property_name: name of the property to check
object_name: name of the object in which we check a specific property
Let us see an example:
//defining an object employee let employee={ empId:2301, name: "Joshua", email:"[email protected]", age:32 }; //both the statements below return true console.log(typeof(employee.empId)); console.log(typeof(employee.name)); //below statement returns false console.log(typeof(employee.rollno));
Output:
number
string
undefined
In this article, we check the type of various object properties to see if they belong to an object or not using the typeof operator. We see that the properties “empId” and “name” belong to the object and hence they return number and string as types respectively. On the other hand, the “rollno” property doesn’t belong to the object and hence returns undefined. We have got the desired result.
Conclusion
In this article, We discussed various methods how to check if the JavaScript object has a property. Some of these include using the “in” operator, using the hasOwnPropertyMethod(), and the typeof operator. Also, learn how to check if the key exists in a JavaScript object.