14

Probably a duplicate of this question.

Silly javascript question: I want to check if an object is the emtpy object.

I call empty object the object that results from using the empty object literal, as in:

 var o = {};

As expected, neither == nor === work, as the two following statements

 alert({}=={});
 alert({}==={});

give false.

Examples of expressions that do not evaluate to the empty object:

  • 0
  • ""
  • {a:"b"}
  • []
  • new function(){}

So what is the shortest way to evaluate for the empty object?

Community
  • 1
  • 1
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • An interesting question. A quick tap in the search engine of my choice has shown that its not actually that easy! This thread might help a little tho : http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object-from-json – Mongus Pong Feb 26 '10 at 09:24
  • Does this answer your question? [How do I test for an empty JavaScript object?](https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object) – McKay Dec 27 '19 at 21:46

5 Answers5

20

You can also use Object.keys() to test if an object is "empty":

if (Object.keys(obj).length === 0) {
  // "empty" object
} else {
  // not empty
}
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
18
function isEmpty(o){
    for(var i in o){
        if(o.hasOwnProperty(i)){
            return false;
        }
    }
    return true;
}
Li0liQ
  • 11,158
  • 35
  • 52
2

You can use this syntax

if (a.toSource() === "({})")

but this doesn't work in IE. As an alternative to the "toSource()" method encode to JSON of the ajax libraries can be used:

For example,

var o = {};
alert($.toJSON(o)=='{}'); // true

var o = {a:1};
alert($.toJSON(o)=='{}'); // false

jquery + jquery.json

starikovs
  • 3,240
  • 4
  • 28
  • 33
0

There is not really a short way to determine if an object is empty has Javascript creates an object and internally adds constructor and prototype properties of Object automatically.

You can create your own isEmpty() method like this:

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

So, if any object has any property, then the object is not empty else return true.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0
javascript:
     cs = 'MTobj={   }; JSON.stringify(MTobj)=="{}"';
     alert(cs+'  is  '+eval(cs));
     cs = 'MTnot={a:2}; JSON.stringify(MTnot)=="{}"';
     alert(cs+'  is  '+eval(cs));

says

MTobj={   }; JSON.stringify(MTobj)=="{}"  is  true
MTnot={a:2}; JSON.stringify(MTnot)=="{}"  is  false

Caveat! Beware! there can be false positives!

javascript:
     cs = 'MTobj={ f:function(){} }; JSON.stringify(MTobj)=="{}"';
     alert(cs+'  is  '+eval(cs));
     alert("The answer is wrong!!\n\n"+
                    (cs="JSON.stringify({ f:function(){} })")+
                             "\n\n  returns\n\n"+eval(cs));
Ekim
  • 949
  • 1
  • 8
  • 9
Ekim
  • 353
  • 2
  • 4