Check if an Object Property is Undefined in JavaScript

avatar

Borislav Hadzhiev

Sun Oct 17 20212 min read

Check if an Object Property is Undefined #

To check if an object property is undefined, use the typeof operator on the object property. The typeof operator returns a string indicating the type of a value. The operator returns the string undefined if the property does not 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 is undefined on the object.

The typeof operator returns a string that indicates the type of a specific 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'

Note that the condition in our if statement would also be met, if the object contains the age property and its value is 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 will be sufficient most of the time, however if you want to check if the property doesn't exist on the object at all, you can 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.

In the example, our else block runs, because the property age 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.

To check if an object property is undefined, use strict equality (===) operator to compare the object property to 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'); }

This approach achieves the same goal, however we directly check if the property is equal to undefined, instead of checking its type.

The strict equality (===) operator checks if the values on the left and right hand side are equal and returns true if they are, and false if they aren't.

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

When using the strict equality (===) operator, any values that are not of the same type are not equal.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee