There are times when you need to determine if an object actually contains anything.
Distinguishing {} from { foo:bar } cannot be done by checking for .length, as an Object does not provide that.

Therefore, the only way to determine if there is anything in the Object is to basically do a for-each loop on the Object itself. Now, depending on the Javascript library you are (probably) using, there will be lots of *stuff* inside the Object that was inherited from the prototype chain. Since we are looking for actual stuff, we utilize the rarely used hasOwnProperty method to determine if the *stuff* in the Object was inherited or actually a part of theObject itself.

Now, you could add this to the prototype of Object itself (for instance Object.prototype.isEmpty = function() { blah};, but I consider that bad practice as – particularly with the Object Object, from which every other type of *thing* in Javascript inherits, the prototype wil be added to all other types of Objects in Javascript as well (for instance String would then have an isEmpty method).

This is all fine and dandy if you want to “robustify” this to support all data types, but trust me, it’s much cleaner and nicer to the DOM
to do it the way I illustrate below.

////////////////////////////////////////////////////////////////////////////////
//
// objectIsEmpty( object) - determines if an Object is empty
// ======================
// You could add this to the prototype chain of Object as .isEmpty(),
// but that would be wasteful. Better to keep it as a standalone function
//
////////////////////////////////////////////////////////////////////////////////
function objectIsEmpty( oObject ) {
var eachProperty = null;
for (eachProperty in oObject) {
if (oObject.hasOwnProperty(eachProperty)) {
return false;
}
}
return true;
}

Our Mission

Render Corporation, founded in 1997, specializes in providing top Web Development talent to some of the most dynamic brands in the world.
We are experts in HTML/HTML5, CSS/CSS3 and Javascript, boasting a client list that includes nearly all of the top names in Silicon Valley.
Chances are, if you are a Javascript developer, you are already using code we developed.
We are actively seeking new clients, so if you require the best, contact us.