Skip to content

Instantly share code, notes, and snippets.

@lerua83
Created November 14, 2013 08:45
Show Gist options
  • Save lerua83/7463506 to your computer and use it in GitHub Desktop.
Save lerua83/7463506 to your computer and use it in GitHub Desktop.
Javascript - How to know if an object is Empty URL: http://stackoverflow.com/questions/4994201/is-object-empty Assuming that by empty means "has no properties of its own"
// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment