Empty Objects and Arrays

New empty objects can be created as var obj = {};. This is the object initializer. If you were to create another object with the initializer and compare the two, they would not be equal.

var obj1 = {};  
var obj2 = {};  
obj1 === obj2; // returns false  

The same goes for arrays created with empty brackets. JavaScript arrays are essentially Javascript objects with numerical keys.

Since objects do not have a .length property, like arrays do, it may be necessary to implement a function to determine if an object is empty. To check if an array is empty, you check if array.length === 0. The following function returns true if the object has no enumerable properties.

function emptyObject(obj) {  
  for (var prop in obj) {
    if (Object.prototype.propertyIsEnumerable.call(obj, prop)) {
      return false;
    }  
  }
  return true;
}