_g3x9q
Last Updated: October 03, 2019
·
686.5K
· 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).

13 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

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

@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.

over 1 year ago ·
29788
const isEmpty = function (input) {
  if (typeof input === 'array') {
    return input.length === 0;
  }

  return !input || Object.keys(input).length === 0;
}
over 1 year ago ·
29822

JSON.stringify(myObj) == "{}"

over 1 year ago ·
31464

The Object.prototype.isEmpty approach shouldn't be used.
I used that with jQuery and it made $.ajax failed.
Took me more than an hour to solve it.

10 months ago ·
31632

if(myObj.hasOwnProperty(key)) {
// true;
} else {
// false
}

9 months ago ·
32298

it doesn't work if it comes:

let obj2 = {
    [Symbol('name')]: 'alice'
}

let obj3 = Object.defineProperty({}, 'name', {
    value: 'alice',
    enumerable: false
})

i think we can use this:

const isEmptyObj = object => !Object.getOwnPropertySymbols(object).length && !Object.getOwnPropertyNames(object).length
6 months ago ·
34624

Why not to use _.isEmpty() from the lodash library?

8 days ago ·