You want to check if an object, for example, an object returned from a fetch request, is empty. How do you do this?
There are various ways to check if an object is empty. The method you choose to use depends on whether you know if the value to check will be an object. The methods also have different efficiencies in terms of operations per second, which could be important if you are checking many values.
Object.keys
If you know the value you are checking is an object, you can use Object.keys
to return an array of a passed-in object’s property names. If the object is empty, the length of the returned array will be 0:
Object.keys(value).length === 0;
for...in
LoopYou can use a for...in
loop in a function to check if an object is empty. Pass in the value as an argument and loop through each property in the value:
function isEmpty(value) { for (let prop in value) { if (value.hasOwnProperty(prop)) return false; } return true; }
For each property, check if the property exists. If there is a property, the function will return false
. If there are no properties on the object, the function returns true
. This method was used as an alternative to using Object.keys
before it was added to JavaScript in the 2011 ECMAScript 5 specification and is widely supported by browsers.
JSON.stringify
You can use JSON.stringify()
to convert the value to a JSON string to check if the value is an empty object. It will be equal to an empty object string if it is:
JSON.stringify(value) === '{}'
This method is slower than the other methods.
If you don’t know if the value is an object, you’ll need to add some extra checks to determine whether it is. First, check if the value is null
or undefined
:
value && Object.keys(value).length === 0;
If you don’t do this check, you’ll get the following error if the value of value
is null
or undefined
:
Cannot convert undefined or null to object
You can end up with false positives if the value is a JavaScript object constructor, such as new Date()
or new RegExp()
:
function isEmpty(value) { return value && Object.keys(value).length === 0; } console.log(new Date()); // Date Tue Nov 29 2022 18:18:10 GMT+0200 (South Africa Standard Time) console.log(isEmpty(new Date())); // true
To fix this, you can also check that the constructor of the value is an object:
value.constructor === Object;