Last active
September 3, 2019 22:59
-
-
Save masautt/8733ba56b12bfa45d8dc45fe5a9dd712 to your computer and use it in GitHub Desktop.
How to check for empty Object in JavaScript?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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