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?
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:
But integer 0 evaluates to false, so you can do:
So:
However you do seem to be getting arrays and objects confused. In JavaScript, we have objects:
And we have arrays:
You can’t do what you do in your opener: