Use Object.keys(obj).length to check if it is empty.
Output : 3
Source: Object.keys()
Use Object.keys(obj).length to check if it is empty.
Output : 3
Source: Object.keys()
You can use Object.keys like this:
class Brand { }
const brand = new Brand();
if (Object.keys(brand).length === 0) {
console.log("No properties")
}
If you want to check if the object has at least one non-null, non-undefined property:
- Get all the values of the object in an array using
Object.values() - Check if at least one of has value using
some
const hasValues =
(obj) => Object.values(obj).some(v => v !== null && typeof v !== "undefined")
class Brand { }
const brand = new Brand();
if (hasValues(brand)) {
console.log("This won't be logged")
}
brand.name = null;
if (hasValues(brand)) {
console.log("Still no")
}
brand.name = "Nike";
if (hasValues(brand)) {
console.log("This object has some non-null, non-undefined properties")
}
Videos
I have been having fun trying to build a type to check if a TS type is empty or inhabited. Initially I thought that I could just check if the type extends never but it turns out that empty object types like {foo: never} don't extend never and similarly, empty tuple types like [string, never] also don't extend never.
So I built a type that determines if a type is empty by recusively checking the properties and handling the edge cases. I learned a lot about the subtype relations and about how conditional types work. Figuring out the distribution over unions was particularly tricky.
I'm sure I've missed some edge cases, please let me know if the comments if you find it useful/interesting or if there is something that doesn't work as expected.
Github
TS Playground
You can use a forโฆin loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:
function isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:
function isEmptyObject(value) {
if (value == null) {
// null or undefined
return false;
}
if (typeof value !== 'object') {
// boolean, number, string, function, etc.
return false;
}
const proto = Object.getPrototypeOf(value);
// consider `Object.create(null)`, commonly used as a safe map
// before `Map` support, an empty object as well as `{}`
if (proto !== null && proto !== Object.prototype) {
return false;
}
return isEmpty(value);
}
Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.
Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).
For compatibility with JavaScript engines that donโt support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:
function isEmpty(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}
Many popular libraries also provide functions to check for empty objects:
jQuery:
jQuery.isEmptyObject({}); // true
lodash:
_.isEmpty({}); // true
Underscore:
_.isEmpty({}); // true
Hoek:
Hoek.deepEqual({}, {}); // true
ExtJS:
Ext.Object.isEmpty({}); // true
AngularJS (version 1):
angular.equals({}, {}); // true
Ramda:
R.isEmpty({}); // true
If ECMAScript 5 support is available, you can use Object.keys():
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}