Last Updated: September 27, 2024
·
1.099M
· Kyle Ross

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

Related protips:

javascript foreach

18 Responses
Add your response

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

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 ·

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

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

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 ·

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

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 ·
const isEmpty = function (input) {
  if (typeof input === 'array') {
    return input.length === 0;
  }

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

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

over 1 year ago ·

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.

over 1 year ago ·

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

over 1 year ago ·

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
over 1 year ago ·

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

over 1 year ago ·

thanks! I struggled with this for a very long time.

over 1 year ago ·

A lot of cool ways have been shown here. Thanks for the advice ;)

over 1 year ago ·

Thanks for the many ways to overcome the problem. It really came in handy.

over 1 year ago ·

Thx for imformation

over 1 year ago ·

How about

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

?

8 months ago ·