How do you check if an object is empty?
By FrontendPro Editorial Team Updated 9/26/2026
Answer
Object.keys(obj).length === 0 is the usual answer, and it is right for plain data objects:
const isEmpty = (obj) => Object.keys(obj).length === 0;
isEmpty({}); // true
isEmpty({ a: 1 }); // false
Why obj === {} never works
Objects compare by reference, so a fresh literal is never equal to anything. JSON.stringify(obj) === "{}" does work but is slow and misleading — it also reports { a: undefined } as empty, because undefined values are dropped during serialisation.
What Object.keys misses
It returns only own, enumerable, string keys. An object can be non-empty in ways it does not see:
Object.keys({ [Symbol("id")]: 1 }).length; // 0
Object.keys(Object.defineProperty({}, "a", { value: 1 })); // [] — non-enumerable
For a stricter check:
const isTrulyEmpty = (obj) => Reflect.ownKeys(obj).length === 0;
Reflect.ownKeys includes symbols and non-enumerable keys.
Guard the input
Object.keys(null) throws, so check the value is an object first:
const isEmptyObject = (v) =>
v !== null && typeof v === "object" && Object.keys(v).length === 0;
Other types need other checks
arr.length === 0; // arrays — Object.keys works but is wasteful
map.size === 0; // Map and Set have .size, not .length or keys
str.trim() === ""; // strings
Object.keys(new Map([["a", 1]])).length is 0, because Map entries are not properties — a subtle bug if a generic "isEmpty" helper is used on one.
For a quick exit on large objects, for (const k in obj) return false stops at the first key instead of building an array, though the difference rarely matters in practice.
