Skip to content

Instantly share code, notes, and snippets.

@masautt
Last active September 3, 2019 22:59
Show Gist options
  • Save masautt/8733ba56b12bfa45d8dc45fe5a9dd712 to your computer and use it in GitHub Desktop.
Save masautt/8733ba56b12bfa45d8dc45fe5a9dd712 to your computer and use it in GitHub Desktop.
How to check for empty Object in JavaScript?
//ES 7+
Object.entries(obj).length === 0 && obj.constructor === Object
//ES 5+
Object.keys(obj).length === 0 && obj.constructor === Object
//ES 5-
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop)) {
return false;
}
}
return JSON.stringify(obj) === JSON.stringify({});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment