15

I'm having an interesting issue that I'm sure is easily explained, but the explanation is eluding me.

An undefined or null object in javascript is equal to false.

var x;
alert(!x); //returns true
alert(x==true); //returns false

What about an empty array object? Is that the equivalent of true or false?

var x = [];
alert (x==true); //returns false
alert (!x); //returns false

If it is equivalent to true, how do I check if it's non-empty? I was hoping to do

if (!x) {
    //do stuff
}

I tried checking x.length, but I'm using this object as a map:

var x = [];
alert(x.length); //returns 0
x.prop = "hello"; 
alert(x.length); //still returns 0

How can I check if my map is empty?

Jon Adams
  • 22,969
  • 17
  • 78
  • 115
chama
  • 5,537
  • 13
  • 57
  • 72
  • 1
    possible duplicate of [is object empty?](http://stackoverflow.com/questions/4994201/is-object-empty) – stusmith Aug 21 '12 at 14:52

6 Answers6

14

It's not as easy as it looks, you have to check that the object has at least one property.

jQuery provides isEmptyObject for that purpose:

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

Sample usage:

> var x = [];
> x.prop = "hello"; 
> isEmptyObject(x);
false
João Silva
  • 81,431
  • 26
  • 144
  • 151
7

Little confused as you seem to be mixing objects and arrays in your question. Hopefully this might help clear it up for you.

An empty array evaluates to true:

!![] //true

But integer 0 evaluates to false, so you can do:

!![].length //false

So:

if([].length) {
   //array has 1 element at least
} else {
  //array has 0 elements
}

However you do seem to be getting arrays and objects confused. In JavaScript, we have objects:

var x = {};
x.foo = "bar";
x.baz = 2;

And we have arrays:

var x = [];
x.push("foo");
x.length //1

You can't do what you do in your opener:

var x = []; //x is an array
x.foo = "bar"; //can't do this on an array
x.length; // 0 
Jack Franklin
  • 3,603
  • 6
  • 24
  • 34
  • Are browsers lenient with this? I do that all over my code and nothing's complained... – chama Aug 21 '12 at 15:01
  • You can do `var x = [], x.foo = 2` and you wont get an error, but nothing will happen. `x` will still be an empty array. – Jack Franklin Aug 21 '12 at 15:02
  • But I can still access `x.foo` – chama Aug 21 '12 at 15:03
  • 1
    Yes you can, but it's not something you should be doing. Either use arrays as arrays, or objects as objects, and accept the pros and cons of each one. Don't mix and match like that! Just to clarify: http://jsfiddle.net/yG5cf/ – Jack Franklin Aug 21 '12 at 15:05
4

if you are using JQuery

jQuery.isEmptyObject(obj)

If not, u can implement your own

isEmptyObject = function(obj) {
for (key in obj) {
    if (obj.hasOwnProperty(key)) return false;
}
return true;
};
Vinícius Fagundes
  • 1,497
  • 12
  • 22
wannas
  • 359
  • 3
  • 11
3

If you're using a Proper JS Map<K, V> Object, you can simply use the Map API on Mozilla Docs.

Specifically there is a Map.prototype.size property, as well as Map.prototype.entries() method, which I believe returns an Array [] of keys. If that is length 0 then the map is empty.

Decoded
  • 808
  • 10
  • 15
2

First of all, you are using an Array not a map. A map would be an Object instance:

var x = {};
x.prop = "5";

Your code is valid since Arrays are also Objects. You can check if your Object is empty as answered in this question.

Community
  • 1
  • 1
Bikush
  • 604
  • 6
  • 22
0

*If of an empty list will give true (if (array1)).

*x.prop does not really add any elements to the array, it just assigns value "hello" to property x of the array but the array is still empty. So you should still use .length to check is array is empty.

Y2H
  • 2,163
  • 1
  • 15
  • 33