JSFiddle: https://jsfiddle.net/pd08dgxu/1/
I need to check whether a JavaScript object is empty or not. Suppose, in this fiddle, Obj1 is not empty (it contains the fields below) but Obj2 is {}
.
In both cases, when I check obj1.length
or obj2.length
, I get Undefined (see alert). Shouldn't we check for existence by using the .length
operator on all variables, whether strings or complex objects?
function getObject() {
return { 'color' : 'red',
'title' : 'my title'
};
}
var myObj1 = getObject();
var myObj2 = {}; //empty object
alert('myObj1 Length = ' + myObj1.length + ' myObj2 Length = ' + myObj2.length);