3 Ways to Check If a Property Exists in an Object

Summary: in this tutorial, you will learn how to check if a property exists in an object.

JavaScript provides you with three common ways to check if a property exists in an object:

  • Use the hasOwnProperty() method.
  • Use the in operator.
  • Compare property with undefined.

Use the hasOwnProperty() method

The JavaScript Object.prototype has the method hasOwnProperty() that returns true if a property exists in an object:

let result = targetObject.hasOwnProperty(propertyName);Code language: JavaScript (javascript)

The following example declares a person object:

let person = {
   firstName: 'John',
   lastName: 'Doe'
};Code language: JavaScript (javascript)

And the following uses the hasOwnProperty() method to check if the firstName property exists in the person object:

let result = person.hasOwnProperty('firstName');
console.log(result); // trueCode language: JavaScript (javascript)

However, the age property does not exist in the person object, therefore, the following code returns false:

let result = person.hasOwnProperty('age');
console.log(result); // falseCode language: JavaScript (javascript)

Note that the hasOwnProperty() looks for the property in the own properties of the object.

For example, all objects inherit the toString property of the Object, the hasOwnProperty() method does not detect it as a property:

let result = person.hasOwnProperty('toString'); 
console.log(result); // falseCode language: JavaScript (javascript)

Use the in operator

The in operator returns true if a property exists in an object. If a property does not exist in the object, it returns false.

propertyName in targetObject

The following example uses the in operator to check if the firstName and age properties exist in the person object:

let person = {
   firstName: 'John',
   lastName: 'Doe'
};

let result = 'firstName' in person; 
console.log(result); // true

result = 'age' in person;
console.log(result); // falseCode language: JavaScript (javascript)

Unlike the hasOwnProperty() method, the in operator looks for the property in both own properties and inherited properties of the object.

The following example uses the in operator to check if the toString property exists in the person object. It returns true because the toString is an inherited property of the person object.

let person = {
   firstName: 'John',
   lastName: 'Doe'
};

let result = 'toString' in person;
console.log(result); // trueCode language: JavaScript (javascript)

Comparing the property with undefined

When you access a non-existing property of an object, you will get undefined. Therefore, you can compare the property with the undefined to check if a property exists in an object:

let person = {
   firstName: 'John',
   lastName: 'Doe'
};

let result = person.firstName !== undefined;
console.log(result); // trueCode language: JavaScript (javascript)

If an object has a property whose value is undefined, then comparing the property with undefined will return an incorrect result. For example:

let person = {
   firstName: 'John',
   lastName: 'Doe',
   age: undefined
};

let result = person.age !== undefined;
console.log(result); // falseCode language: JavaScript (javascript)

In this example, the age property does exist in the person object. However, its initial value is undefined. Therefore, comparing the person.age with undefined returns false , which is not expected.

Summary

  • Use the hasOwnProperty() method to check if an property exists in the own properties of an object.
  • Use the in operator to check if a property exists in both own properties and inherited properties of an object.
  • Compare the property with undefined to check if a property exists only when you are sure that the initial value of the property is not undefined.
Was this tutorial helpful ?