Categories
JavaScript Answers

How to Check if an Object is Empty in JavaScript?

Spread the love

Checking for an empty object is something that we might have to do sometimes.

In this article, we’ll look at various ways we can check if an object is empty with JavaScript.

Object.keys and the constructor Property

We can combine the Object.keys method and the constructor property to check if an object is an empty object.

To do this, we write:

const obj = {}
console.log(obj &&
  Object.keys(obj).length === 0 && obj.constructor === Object)

obj makes sure that obj isn’t null or undefined .

Object.keys(obj).length === 0 checks that the number of keys in obj is 0.

Object.keys returns array of non-inherited keys of an object.

However, we don’t have any check to see if obj is an object literal.

And so, we need to write:

obj.constructor === Object

to check that.

If it’s an object literal, then it should be created from the Object constructor.

So obj.constructor should return Object is obj is an object literal.

This only one way to check for an empty object.

There are few other ways to do this.

JSON.stringify

We can check for an object with the JSON.stringify method.

For instance, we can write:

function isEmpty(obj) {
  for (const prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      return false;
    }
  }

  return JSON.stringify(obj) === JSON.stringify({});
}

We loop through the ketys with the for-in loop.

Then we check if an object has any non-inherited properties with hasOwnProperties .

If obj.hasOwnProperties returns true with any prop value, which has the key that’s being iterated through, then we should return false .

This is because this means a non-inherited key exists in obj and obj isn’t empty.

If the loop finds no keys, then we can use JSON.stringify to check for an stringified empty object.

This is good for supporting runtime environments that don’t have the Object.keys method, which should be rare.

Lodash isEmpty Method

The isEmpty method comes with Lodash and it lets us check if an object is empty.

We just pass in an object we want to check:

_.isEmpty({});

Object.getOwnPropertyNames

The Object.getOwnPropertyNames method returns an array of non-inherited keys of an object.

So we can use it like Object.keys to check for an empty object.

For instance, we can write:

if (Object.getOwnPropertyNames(obj).length === 0) {
  //...
}

We just check if the returned array has 0 length.

It also returns non-enumerable properties, which means it returns keys that won’t be returned by Object.keys , but aren’t inherited from any prototype object.

Therefore, this check is more comprehensive than checking with Object.keys .

Conclusion

There are several ways to check for an empty object with JavaScript.

The best ways are to use Object.keys or Object.getOwnPropertyNames .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *