JavaScript provides three different ways to check if a property exists in an object or not:
The in
operator provides you with the easiest way to check if a property exists in an object. If a property exists in an object, then the in
operator returns true
. On the other hand, if a property does not exist in the object, then the in
operator returns false
.
'propertyName' in objectVariable
The following example shows how to check if age
and salary
properties exist in the employee
object:
let employee = { firstName: "Mohit", lastName: "Natani", salary: 100000 }; console.log('age' in employee); //false console.log('salary' in employee); //true if('age' in employee){ console.log("age property exists in employee."); }else{ console.log("age property does not exist in employee."); } if('salary' in employee){ console.log("salary property exists in employee."); }else{ console.log("salary property does exist in employee."); }
Output
false true age property does not exist in employee. salary property exists in employee.
The best thing about the in
operator is that it looks for the inherited properties of an object, whereas the hasOwnProperty()
method does not.
The following example shows how to use the in
operator to check if the inherited property toString
exists in the employee
object. The in
operator returns true
because all objects inherit the toString
method from the Object
.
let employee = { firstName: "Mohit", lastName: "Natani", salary: 100000 }; console.log('toString' in employee); //true
Object.prototype
provides hasOwnProperty()
method that returns true
if a property exist in an object. If a property does not exist in an object, then the hasOwnProperty() method returns false
.
objectVariable.hasOwnProperty('propertyName')
The following example uses the hasOwnProperty()
method to check if age
and salary
properties exist in the employee
object.
let employee = { firstName: "Mohit", lastName: "Natani", salary: 100000 }; console.log(employee.hasOwnProperty('age')); //false console.log(employee.hasOwnProperty('salary')); //true
The hasOwnProperty()
method only looks for the property that is owned by the object. It does not look for inherited properties of an object. It returns false
if tested on the inherited properties.
The following example shows that the hasOwnProperty()
method returns false
when tested on the toString
property. The reason for this is that the toString
is an inherited property.
let employee = { firstName: "Mohit", lastName: "Natani", salary: 100000 }; console.log(employee.hasOwnProperty('toString')); //false
In JavaScript, when you access a property that does not exist in an object, then you get undefined
. You can use this information to test if a property exists in an object or not.
let employee = { firstName: "Mohit", lastName: "Natani", salary: 100000 }; if(employee.age === undefined){ console.log("age property does not exist in employee."); }else{ console.log("age property exists in employee."); } if(employee.salary === undefined){ console.log("salary property does not exist in employee."); }else{ console.log("salary property exists in employee."); }
Output
age property does not exist in employee. salary property exists in employee.
You cannot rely on this way of checking because if a property has an undefined
value, in that case, you will get an incorrect result.
let employee = { firstName: "Mohit", lastName: "Natani", salary: undefined }; if(employee.salary === undefined){ console.log("salary property does not exist in employee."); }else{ console.log("salary property exists in employee."); }
Output
salary property does not exist in employee.
In this example, the salary
property exists in the employee
object and has an undefined
value. So, when compared with undefined
gave true, which is not expected.
Note: To learn about undefined data type in JavaScript, visit What is undefined in JavaScript?