How to check if an object is empty in JavaScript — and why Date and Map look empty

The server returned a response. If there's no data, you need to show a "Nothing here yet" placeholder. You write:
if (!data) showEmptyState()
The server sends {}. No placeholder. Instead — a blank screen with not a single error.
The thing is, {} in JavaScript is a truthy value. An empty object is still an object, and if (data) treats it as "present". Comparing won't work either: data === {} is always false, because two objects are only equal if they're the very same object.
So "emptiness" has to be checked explicitly. Below — step by step, from the most common way to the reliable one. Along the way you'll see where each of them lies.
Step 1. Check it's an object at all
The first error happens before the emptiness check:
Object.keys(null)
// TypeError: Cannot convert undefined or null to object
If the response can arrive as null or undefined, filter those out first. Where these values come from is covered in what is null.
if (data == null) return showEmptyState() // catches both null and undefined
Step 2. Count the keys
The most popular way:
Object.keys(data).length === 0
Object.keys returns an array of the object's own keys. Empty array — empty object. For plain objects from JSON this is enough 90% of the time. How keys and values work is in what is an object.
But this method has blind spots. It only looks at ordinary enumerable keys. So:
Object.keys(new Date()).length // 0 — the date is "empty"
Object.keys(new Map([[1, 2]])).length // 0 — a Map with data is "empty"
Object.keys(new Set([1])).length // 0 — same
Dates, Map and Set keep their data outside ordinary keys. Map and Set have their own property for this — .size.
Also, an array passes as an object: Object.keys([]) gives [] too. If you specifically expect an object, check Array.isArray(data) separately.
Step 3. Don't trust JSON.stringify
The second popular tip from the internet:
JSON.stringify(data) === '{}'
It lies in two places at once:
JSON.stringify({ name: undefined }) // '{}' — but the key is there
JSON.stringify(new Map([[1, 2]])) // '{}' — a Map with data
JSON silently drops undefined and functions. And on a big object this check is slow too: it builds the whole string just to learn whether there's anything at all. More on what JSON can hold in what is JSON.
Step 4. Don't pull in lodash for one check
Lodash's _.isEmpty is handy, but it has its own idea of emptiness:
_.isEmpty({}) // true
_.isEmpty(1) // true — a number is "empty"
_.isEmpty(true) // true
If a number arrives in the function by mistake, the check says "empty" and raises no alarm. No need to bring in a library for this — four lines of your own are enough.
Step 5. Build a reliable function
function isEmptyObject(value) {
if (value === null || typeof value !== 'object') return false
if (Array.isArray(value)) return value.length === 0
if (value instanceof Map || value instanceof Set) return value.size === 0
return Reflect.ownKeys(value).length === 0
}
What it does:
- Non-objects (
null, numbers, strings) aren't "empty objects", so returnfalse. That way a type error doesn't get hidden. - An array is empty if it has no elements.
MapandSetare checked by.size.- For everything else,
Reflect.ownKeys— it sees ordinary keys, hidden ones, and symbol keys thatObject.keysskips.
This function honestly calls { name: undefined } non-empty: the key exists. If in your task "a key holding undefined" means empty, check the values: Object.values(value).every(v => v === undefined).
What you get
isEmptyObject({}) // true
isEmptyObject({ a: 1 }) // false
isEmptyObject(null) // false
isEmptyObject([]) // true
isEmptyObject(new Map()) // true
isEmptyObject(new Map([[1, 2]])) // false
isEmptyObject(new Date()) // false
And the code from the start becomes clear:
if (data == null || isEmptyObject(data)) showEmptyState()
A similar story happens with arrays that "should be filled by now". It's covered in why my array is empty.
Why doesn't if (obj) catch an empty object?
Because every object in JavaScript is truthy, even {} and []. There are few falsy values: false, 0, '', null, undefined, NaN and a couple of exotic ones like 0n.
How do I check for an empty object in React?
Same as anywhere: Object.keys(data).length === 0 or the function above. Just run the check before rendering the list, so you show a placeholder rather than an empty block.
How do I check for an empty object in TypeScript?
Use the same function. In TypeScript the type {} means "any non-null value", not "an empty object". An empty object type is written as Record<string, never>.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





