Skip to content

Instantly share code, notes, and snippets.

@slavafomin
Last active January 10, 2022 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save slavafomin/c7ab7f7e45431748ad8c to your computer and use it in GitHub Desktop.
Save slavafomin/c7ab7f7e45431748ad8c to your computer and use it in GitHub Desktop.
JavaScript is Object Empty Function
/**
* Returns true if specified object has no properties,
* false otherwise.
*
* @param {object} object
* @returns {boolean}
*/
function isObjectEmpty(object)
{
if ('object' !== typeof object) {
throw new Error('Object must be specified.');
}
if (null === object) {
return true;
}
if ('undefined' !== Object.keys) {
// Using ECMAScript 5 feature.
return (0 === Object.keys(object).length);
} else {
// Using legacy compatibility mode.
for (var key in object) {
if (object.hasOwnProperty(key)) {
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment