To check for an empty object in JavaScript, first define empty as “no own enumerable string-keyed properties.” For a value already known to be a plain object, Object.keys(obj).length === 0 is the direct test. To safely check an empty object in JavaScript when input may be null or another type, add guards before calling Object.keys().
A JavaScript empty object can still have inherited, symbol, or non-enumerable properties, so the result depends on which properties your application considers.
How to check for an empty object in JavaScript with Object.keys(obj).length === 0
Object.keys() returns an array containing an object’s own enumerable string keys. An object literal with no such keys produces an empty array:
const value = {};
Object.keys(value).length === 0; // true
An object with an own enumerable property produces a nonzero length:
const value = { name: “Ada” };
Object.keys(value).length === 0; // false
When the input is expected to be an object but may be null, use a type and null guard:
const isEmpty = value =>
value !== null &&
typeof value === “object” &&
Object.keys(value).length === 0;
This condition also returns true for arrays, dates, maps, sets, and some class instances that have no enumerable own string properties. Use the stricter plain-object check below when the value’s type is not already controlled.
Guard an empty JavaScript object check against null, arrays, Map, Set, Date, and class instances
typeof null is “object”, but Object.keys(null) throws a TypeError. Check for null before the type test. Arrays also have object types, so a separate array check or a plain-object prototype check is needed.
This function accepts object literals and objects created with a null prototype, while rejecting arrays and built-in or custom instance types:
const isEmptyPlainObject = value =>
value !== null &&
typeof value === “object” &&
(Object.getPrototypeOf(value) === Object.prototype ||
Object.getPrototypeOf(value) === null) &&
Object.keys(value).length === 0;
An array should usually be tested with value.length === 0. A Map and a Set store data in internal collection slots, so use map.size === 0 or set.size === 0. A Date is a date value rather than an empty record. A class instance may have no own fields while still representing a meaningful domain object; test its required fields or use an instanceof check before applying an emptiness rule.
What does a JavaScript empty object mean for own, inherited, symbol, and non-enumerable properties?
Object.keys() counts only own, enumerable, string-named properties. Each category changes the result:
- Own properties: properties stored directly on the object. Enumerable own properties appear in Object.keys().
- Inherited properties: properties found on the object’s prototype. They do not appear in Object.keys(), and this is normally desirable for configuration or data objects.
- Symbol properties: own symbol-keyed properties are not returned by Object.keys(), even when they are enumerable.
- Non-enumerable properties: own string or symbol properties marked non-enumerable are also excluded.
For example, an object can have hidden data while appearing empty to Object.keys():
const value = {};
Object.defineProperty(value, “id”, { value: 7 });
Object.keys(value).length === 0; // true
To count all own string and symbol keys, including non-enumerable keys, use Reflect.ownKeys(value).length === 0. Object.getOwnPropertyNames() includes all own string keys, while Object.getOwnPropertySymbols() returns own symbol keys. A for…in loop is not equivalent to Object.keys() because it can include inherited enumerable properties.
Choose a definition of emptiness when checking whether an object is empty in JavaScript
- For a plain data object, define empty as having no own enumerable string keys and use the guarded Object.keys(value).length === 0 test.
- For an object where symbols or non-enumerable metadata matter, use Reflect.ownKeys(value).length === 0 after validating the value’s type.
- For collections, use their native size or length: Map.size, Set.size, or array length.
- For inherited settings, decide explicitly whether the prototype contributes data. Avoid treating inherited properties as own record fields by accident.
Do not use JSON.stringify(value) === “{}” as a universal emptiness test. Serialization omits symbols, non-enumerable properties, and several values that do not serialize as ordinary object keys. It also says nothing about whether the value is a plain object, so property inspection and type validation are more reliable.









