loading...
Cover image for Different ways to check If Object is empty or not

Different ways to check If Object is empty or not

awesome_aj1298 profile image Arpit Jain ・2 min read

Checking if the Object is empty or not is quite a simple & common task but there are many ways to check the object is empty or not. So in this blog, I am trying to cover all the best ways to check object is empty or not.

So let's figure out how to accomplish it.

Here firstly we create an empty Object using the object literal syntax

const obj = {}

After creating an empty object obj we compare it to another empty object just like this:

console.log(obj === {}) // false

We are getting false. How is this possible??? We compared two empty objects just like this:

console.log({} === {}) // false

This because we are comparing references, not values. The reference to these objects isn't the same, even though the value is the same.

So how can we actually check if an object is empty or not?

Using Object.Keys

Object.keys will return an Array, which contains the property names of the object. If the length of the array is 0, then we know that the object is empty.

function isEmpty(obj) {
    return Object.keys(obj).length === 0;
}

we can also check this using Object.values and Object.entries

This is the simplest way to check if an object is empty.

Using JSON.stringify

If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.

function isEmptyObject(obj){
    return JSON.stringify(obj) === '{}';
}

Looping over object properties with for…in

The for…in statement will loop through the enumerable property of the object.

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

    return true;
}

In the above code, we will loop through object properties and if an object has at least one property, then it will enter the loop and return false. If the object doesn’t have any properties then it will return true.

Using Underscore and Lodash

we can also check by underscore.js like this:

_.isEmpty(obj);

_.isEmpty() is an underscore.js function to check whether a list, array, string, object, etc is empty or not. It first finds out the length of the passed argument and then decides. It returns true if the argument passed is empty, i.e., does not have any elements in it. Otherwise, it returns false.

Using jQuery

we can also check by jQyery library like this:

jQuery.isEmptyObject(obj); 

Thanks for reading 😄.

If there is anything I have missed, or if you have a better way to do something then please let me know.

Posted on by:

Discussion

pic
Editor guide
 

Great article. Just a small question, which one will be the best approach?

 

Thanks 😊, The best approach is utility function

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

    return true;
}
 

I'd love to see a benchmarked comparison : )