Use the object key method to get the length of an object then check if the object is empty in JavaScript. SimpleCheck if the length of keys is equal to 0
, if it is, then the object is empty.
JavaScript check if object is empty
Simple example code Access the length
property on the array and check object empty to not.
<!DOCTYPE html>
<html>
<body>
<script>
const obj = {};
const isEmpty = Object.keys(obj).length === 0;
if (isEmpty) {
console.log("Given object is empty",isEmpty)
}
</script>
</body>
</html>
Output:

An alternative method is to try to iterate over the properties of the object. If there is even a single iteration, then the object is not empty.
// Supported in IE 6-11
const obj = {};
function isEmpty(object) {
for (const property in object) {
return false;
}
return true;
}
console.log(isEmpty(obj)); // 👉️ true
Do comment if you have any doubts or suggestions on this JS object topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.