Return to Snippet

Revision: 63853
at June 13, 2013 02:24 by satie83


Initial Code
// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;

function is_empty(obj) {

    // null and undefined are empty
    if (obj == null) return true;
    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length && obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    for (var key in obj) {
        if (hasOwnProperty.call(obj, key))    return false;
    }

    return true;
}

Initial URL
http://stackoverflow.com/questions/4994201/is-object-empty

Initial Description
Assuming that by empty means "has no properties of its own"

Initial Title
Javascript - How to know if an object is Empty

Initial Tags
javascript

Initial Language
JavaScript