Matthew C.
—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;
If you’re looking to get a deeper understanding of how JavaScript application monitoring works, take a look at the following articles:
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.