Javascript

Check Empty Object in Javascript

If you are also wondering how to check if an object is empty in JavaScript, then you are in the perfect place. This article includes the various methods by which we can easily check empty objects in Javascript.

Suppose you have an value which is equal to the empty object, which can be created using the object literal syntax:

const emptyObject = {}

and you want to check that, this empty object is empty in real or not. Here are the ways by which you can achieve this easily.

Use the Object.entries() function to check empty object

You can use the javascript default Object.entries() function to check if an object is empty or not as this function returns an array containing the object’s enumerable properties. Here is how to use it.

Object.entries(objectToCheck).length === 0

Use the lodash isEmpty() function to check empty objects

If you don’t want to use the Javascript method, you can also use the lodash isEmpty() function to check if an object is empty or not as this function checks if value is an empty object, collection, map, or set. Here is an example of the same.

_.isEmpty(objectToCheck)

So, this is all about how to check the empty objects in Javascript.