_g3x9q
Last Updated: May 16, 2017
·
71.18K
· kyleross

How to check if JavaScript Object is empty

With JavaScript, it can be difficult to check whether an object is empty. With Arrays, you can easily check with myArray.length, but on the other hand, objects do not work that way.

The best way to check if an object is empty is by using a utility function like the one below.

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

So if you have an empty object, you can check whether it is empty by using the above function.

var myObj = {}; // Empty Object
if(isEmpty(myObj)) {
    // Object is empty (Would return true in this example)
} else {
    // Object is NOT empty
}

Alternatively, you can write the isEmpty function on the Object prototype.

Object.prototype.isEmpty = function() {
    for(var key in this) {
        if(this.hasOwnProperty(key))
            return false;
    }
    return true;
}

Then you can easily check if the object is empty like so.

var myObj = {
    myKey: "Some Value"
}

if(myObj.isEmpty()) {
    // Object is empty
} else {
    // Object is NOT empty (would return false in this example)
}

Extending the object prototype is not the best thing to do as it can cause some browser issues and other issues with certain frameworks (it's also not always reliable in certain environments). The example I gave is pretty much framework agnostic although.

This is one of those utility functions that will always be useful, especially if you deal with a lot of objects on a daily basis (like I do).

Say Thanks
Respond

7 Responses
Add your response

1088
var isMyObjectEmpty = !Object.keys(myObject).length;
over 1 year ago ·
1096

Nice tip. Seems like the Underscore.js implementation -->
http://documentcloud.github.com/underscore/#isEmpty

Just that those guys are checking for Arrays and String in the same function like this:

_.isEmpty = function(obj) {
   if (obj == null) return true;
   if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
   for (var key in obj) if (_.has(obj, key)) return false;
   return true;
 };
over 1 year ago ·
1111

@avenger7x Yes, that certainly works although it is not supported in some older browsers such as Firefox < 4 and IE < 9. It's a new addition under ECMAScript 5.

over 1 year ago ·
1112

@develoser Underscore is just one of those overly useful tools out there and I agree and suggest that others check it out. Thanks for you input!

over 1 year ago ·
12463
56c449f1a408dba893793094599f7fb5

Hi, I'm wondering why the use of hasOwnProperty is required?

function isEmpty(arg) {
  for (var item in arg) {
    return false;
  }
  return true;
}
over 1 year ago ·
12874
990aab042d81755e54ab60c4b5f64887

@denisjacquemin: historically, for(x in y) will show "toString" as a value of "x". using hasOwnProperty allows for skipping over "built in" or "metadata" properties, and just evaluate "value" properties.

over 1 year ago ·
28927

If you don't want to copy-paste this all over your different repositories, you can also try this NPM package or this Bit component.

They should work just fine and make it useable anywhere you need.

3 months ago ·