75

From the node REPL thing,

> d = {}
{}
> d === {}
false
> d == {}
false

Given I have an empty dictionary, how do I make sure it is an empty dictionary ?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
João Pinto Jerónimo
  • 8,458
  • 13
  • 57
  • 83
  • 1
    You might consider using a library such as [check-types](https://www.npmjs.com/package/check-types). In which case, you could use `check.emptyObject(d)`. – mareoraft Oct 29 '15 at 00:36
  • 1
    Here is a more up-to-date answer: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – guettli Nov 27 '19 at 15:08

10 Answers10

124
function isEmpty(obj) {
  return Object.keys(obj).length === 0;
}
Raynos
  • 156,883
  • 55
  • 337
  • 385
  • 1
    Only in newer browsers. For the record, the keys property is only supported in IE >= 9 [reference](http://kangax.github.io/compat-table/es5/) – Nick Mitchell Dec 09 '14 at 04:52
  • 6
    I'd be amazed at JavaScript lacking this basic functionality, if I weren't so used to it by now. – Teekin Oct 15 '19 at 11:29
  • Here is a more up-to-date answer: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – guettli Nov 27 '19 at 15:08
19

You could extend Object.prototype with this isEmpty method to check whether an object has no own properties:

Object.prototype.isEmpty = function() {
    for (var prop in this) if (this.hasOwnProperty(prop)) return false;
    return true;
};
Gumbo
  • 594,236
  • 102
  • 740
  • 814
15

How about using jQuery?

$.isEmptyObject(d)
stroz
  • 1,552
  • 1
  • 14
  • 28
10

Since it has no attributes, a for loop won't have anything to iterate over. To give credit where it's due, I found this suggestion here.

function isEmpty(ob){
   for(var i in ob){ return false;}
  return true;
}

isEmpty({a:1}) // false
isEmpty({}) // true
David Ruttka
  • 13,641
  • 2
  • 40
  • 39
9

This is what jQuery uses, works just fine. Though this does require the jQuery script to use isEmptyObject.

isEmptyObject: function( obj ) {
    for ( var name in obj ) {
        return false;
    }
    return true;
}

//Example
var temp = {};
$.isEmptyObject(temp); // returns True
temp ['a'] = 'some data';
$.isEmptyObject(temp); // returns False

If including jQuery is not an option, simply create a separate pure javascript function.

function isEmptyObject( obj ) {
    for ( var name in obj ) {
        return false;
    }
    return true;
}

//Example
var temp = {};
isEmptyObject(temp); // returns True
temp ['b'] = 'some data';
isEmptyObject(temp); // returns False
cevaris
  • 5,208
  • 2
  • 42
  • 32
5

I'm far from a JavaScript scholar, but does the following work?

if (Object.getOwnPropertyNames(d).length == 0) {
   // object is empty
}

It has the advantage of being a one line pure function call.

Steve Bennett
  • 84,226
  • 27
  • 133
  • 175
1
var SomeDictionary = {};
if(jQuery.isEmptyObject(SomeDictionary))
// Write some code for dictionary is empty condition
else
// Write some code for dictionary not empty condition

This Works fine.

1

If performance isn't a consideration, this is a simple method that's easy to remember:

JSON.stringify(obj) === '{}'

Obviously you don't want to be stringifying large objects in a loop, though.

Steve Bennett
  • 84,226
  • 27
  • 133
  • 175
1

You'd have to check that it was of type 'object' like so:

(typeof(d) === 'object')

And then implement a short 'size' function to check it's empty, as mentioned here.

Community
  • 1
  • 1
chrisfrancis27
  • 4,427
  • 1
  • 21
  • 31
0

If you try this on Node.js use this snippet, based on this code here

Object.defineProperty(Object.prototype, "isEmpty", {
    enumerable: false,
    value: function() {
            for (var prop in this) if (this.hasOwnProperty(prop)) return false;
            return true;
        }
    }
);
Peter O.
  • 28,965
  • 14
  • 72
  • 87
arbyter
  • 509
  • 1
  • 4
  • 12