Categories
javascript javascript-objects

How do I test for an empty JavaScript object?

3895

After an AJAX request, sometimes my application may return an empty object, like:

var a = {};

How can I check whether that’s the case?

0

    7129

    ECMA 5+:

    // because Object.keys(new Date()).length === 0;
    // we have to do some additional check
    obj // 👈 null and undefined check
    && Object.keys(obj).length === 0
    && Object.getPrototypeOf(obj) === Object.prototype
    

    Note, though, that this creates an unnecessary array (the return value of keys).

    Pre-ECMA 5:

    function isEmpty(obj) {
      for(var prop in obj) {
        if(Object.prototype.hasOwnProperty.call(obj, prop)) {
          return false;
        }
      }
    
      return JSON.stringify(obj) === JSON.stringify({});
    }
    

    jQuery:

    jQuery.isEmptyObject({}); // true
    

    lodash:

    _.isEmpty({}); // true
    

    Underscore:

    _.isEmpty({}); // true
    

    Hoek

    Hoek.deepEqual({}, {}); // true
    

    ExtJS

    Ext.Object.isEmpty({}); // true
    

    AngularJS (version 1)

    angular.equals({}, {}); // true
    

    Ramda

    R.isEmpty({}); // true
    

    9

    • 47

      Object.keys(new Date()).length === 0; so this answer can be misleading.

      – cjbarth

      Mar 1, 2016 at 16:34

    • Why do we need to check obj.contructor===Object in ECMA5+ code ? We can only use this code obj // 👈 null and undefined check && Object.keys(obj).length === 0

      Jul 8, 2021 at 11:54


    • The comment right above yours literally explains why =) There are a million different objects, so you want to make sure to only test for “actual object primitives”, i.e. objects whose constructor is not some extension of Object.

      Aug 1, 2021 at 3:41


    • This returns true for an object that is not empty, but all its keys are symbols.

      – Nate

      Nov 12, 2021 at 1:10

    • 1

      @Wronski Yes, potentially, and it’s already in the answer. It’s not a very good solution, though (slow, no more reliable and sometimes less).

      – Ry-

      Mar 19 at 22:54


    1240

    If ECMAScript 5 support is available, you can use Object.keys():

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

    For ES3 and older, there’s no easy way to do this. You’ll have to loop over the properties explicitly:

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

    6

    • 69

      This works fine, or more simply: function isEmpty(object) { for(var i in object) { return true; } return false; }

      – niczak

      Mar 24, 2010 at 23:23

    • 53

      Shouldnt true and false be reversed in this function?

      – namtax

      May 5, 2010 at 13:46

    • 44

      @namtax: no – the function is named isEmpty(), so it should return false if it has a property

      – Christoph

      May 6, 2010 at 16:31

    • @NicholasKreidberg That code function isEmpty(object) { for(var i in object) { return true; } return false; } got to be corrected after 11 years. Here’s the correction: function isEmpty(obj) { return !(() => { for (const i in obj) { return true; } return false; })(); }

      Jul 4, 2021 at 8:52


    • 2

      Alernatively: function isObjectEmpty(obj) { for (const i in obj) return false; return true; }

      Jul 5, 2021 at 7:18

    589

    For those of you who have the same problem but use jQuery, you can use jQuery.isEmptyObject.

    3

    • 3

      This won’t work if you (or any plugin) modified Object.prototype.

      Jun 5, 2012 at 9:13

    • note that this works fine for the question asked, {}, but that jQuery.isEmptyObject([]) === true, presumably because an empty list is iterable.

      May 17, 2013 at 16:20

    • 3

      Note that jQuery.isEmptyObject returns false when you feed it an empty jQuery object (as noted on its API page). It only works for regular JavaScript objects.

      Oct 1, 2014 at 15:40