/

Check if JavaScript object is empty

javascript
js
if (obj && Object.entries(obj).length === 0 && obj.constructor === Object) {
// do something
}

or use a function:

ts
export const isObjectEmpty = (obj: unknown): boolean => {
if (obj === null || obj === undefined || obj === false) {
return true
}
return Object.entries(obj).length === 0
}
// isObjectEmpty({}) true
// isObjectEmpty({a: 1}) false

You can also invert that function to check if an object has values.

ts
export const isObjectFull = (obj) => !isObjectEmpty(obj)

Not sure about the naming of this one, but you get the idea

Edit this page
Want to make your own site like this?
Try gatsby-theme-code-notes by Zander Martineau.
Notes on code. My second brain, by Zander Martineau.