Anjan Dutta

How to check if a javascript object property is undefined

How to check if a javascript object property is undefined

To check if a javascript object property is undefined or not, we can compare the object property with the undefined data type.

var person = {};
typeof person.age;
// > undefined

The typeof identifier can get the data type of any variable value.

In the above code snippet, this line typeof person.age checks the type of the object property .age which in turn is an undefined property.

So, the result will be "undefined".

We can use this approach to create more formalized code like below.

var person = {};
if (typeof person.age === 'undefined') {
// age is undefined
}