Check if an Object Property is Undefined in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 23, 2022

banner

Photo from Unsplash

Check if an Object Property is Undefined #

Use the typeof operator to check if an object property is undefined, e.g. if (typeof obj.age === 'undefined') {}. The typeof operator will return the string "undefined" if the property doesn't exist on the object.

index.js
const obj = {name: 'Tom'}; if (typeof obj.age === 'undefined') { console.log('⛔️ age property is undefined'); } else { console.log('✅ age property is defined'); }

We used the typeof operator to check if the age property in the object is undefined.

The typeof operator returns a string that indicates the type of the value.

Here are some examples.

index.js
console.log(typeof ''); // 👉️ 'string' console.log(typeof undefined); // 👉️ 'undefined' console.log(typeof 5); // 👉️ 'number' console.log(typeof {}); // 👉️ 'object' console.log(typeof []); // 👉️ 'object'

The condition in our if statement would also be met, if the object contains the age property with a value set to undefined.

index.js
const obj = {name: 'Tom', age: undefined}; if (typeof obj.age === 'undefined') { // this runs console.log('⛔️ age property is undefined'); } else { console.log('✅ age property is defined'); }

This should be OK most of the time. However, if you want to check if the property doesn't exist on the object at all, use the Object.hasOwnProperty method.

index.js
const obj = {name: 'Tom', age: undefined}; if (!obj.hasOwnProperty('age')) { console.log("⛔️ object doesn't have age property"); } else { // 👉️ this runs console.log("✅ object has age property"); }

The hasOwnProperty method returns true if the object has the property and false otherwise.

The else block runs because the age property exists on the object, even though its value is undefined.

You will very rarely (if ever) see the hasOwnProperty method used in applications. For most use cases, the first approach works perfectly fine.

There is an alternative and a little more concise approach.

Use the strict equality (===) operator to check if an object property is undefined, e.g. if (obj.age === undefined) {}. The strict equality operator will return true if the property is undefined and false otherwise.

index.js
const obj = {name: 'Tom'}; if (obj.age === undefined) { console.log('⛔️ age property is undefined'); } else { console.log('✅ age property is defined'); }
We directly check if the property is equal to undefined, instead of checking what its type is.

The strict equality (===) operator checks if the values on the left-hand and right-hand side are equal and of the same type.

Here are some examples.

index.js
console.log(undefined === undefined); // 👉️ true console.log(null === undefined); // 👉️ false console.log('' === undefined); // 👉️ false console.log('5' === 5); // 👉️ false

Any values that are not of the same type are not equal when using the strict equality (===) operator.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.