An empty JavaScript object has no own enumerable properties — no key-value pairs at all. The fastest and most widely used way to check this is Object.keys(obj).length === 0, which returns an array of the object’s property names and checks whether that array is empty.
Using Object.keys()
This is the standard approach in modern JavaScript. Object.keys() returns an array containing the property names of an object, so checking the array’s length tells you immediately whether the object holds any data.
const obj = {};
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
isEmpty(obj); // true
You can also swap in Object.values() or Object.entries() for the same result, since both methods can also be used to check this in the same way as Object.keys. All three work in any modern browser without extra setup.
Using a for…in Loop
Before Object.keys() became widely supported, developers used a for...in loop to check each property manually. This loops through each property in the value, and if a property exists the function returns false; if none exist, it returns true. It’s more verbose than Object.keys(), but it works in older environments that predate ES5.
function isEmpty(obj) {
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) return false;
}
return true;
}
The hasOwnProperty check matters here — without it, the loop would also pick up inherited properties from the object’s prototype chain, giving a false “not empty” result.
Using JSON.stringify()
Converting the object to a string and comparing it against '{}' is another option, though this method is slower than the Object.keys or for…in approaches.
JSON.stringify(obj) === '{}'
This works reliably for plain objects but can behave unexpectedly with objects containing undefined values or functions, since JSON.stringify skips those properties silently.
Handling null or Undefined Values
If you’re not certain the value you’re checking is actually an object — for example, data returned from an API — checking Object.keys().length directly can throw an error on null or undefined. Guard against this by checking the value first:
value && Object.keys(value).length === 0;
This short-circuits before Object.keys() runs, so null or undefined values return false (not empty) instead of crashing the script.
Using Lodash’s isEmpty()
If your project already includes the Lodash utility library, its built-in method handles the check in one line and covers edge cases like null, arrays, and strings automatically.
const _ = require('lodash');
_.isEmpty(obj); // true or false
This is convenient if you’re already using Lodash elsewhere, but it’s not worth adding the library just for this one check — the native Object.keys() method covers the same ground for plain objects without a dependency.
Join The Discussion
Which method do you reach for when checking empty objects in your own projects, and have you run into any edge cases — like objects with inherited properties or unexpected null values — that changed how you approach this?