Use Object.keys(obj).length to check if it is empty.

Output : 3

Source: Object.keys()

Answer from DeepSea on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ check-if-an-object-is-empty-in-typescript
Check if an Object is Empty in TypeScript - GeeksforGeeks
July 23, 2025 - In this approach, we are using the Object.keys() function in TypeScript to check if an object is empty.
Discussions

Utility type to decide if a type is empty or inhabited
I wonder if this could be useful for writing type-level unit tests? More on reddit.com
๐ŸŒ r/typescript
7
1
June 13, 2024
How do I test for an empty JavaScript object? - Stack Overflow
After an AJAX request, sometimes my application may return an empty object, like: var a = {}; How can I check whether that's the case? More on stackoverflow.com
๐ŸŒ stackoverflow.com
TypeScript empty object for a typed variable - Stack Overflow
Either you want user to be of type ... allow an empty object. Right now, the compiler is correctly telling you that user is not a User. โ€“ jcalz ยท I don't think this should be considered a proper answer because it creates an inconsistent instance of the type, undermining the whole purpose of TypeScript... More on stackoverflow.com
๐ŸŒ stackoverflow.com
why there isn't a method to check an object is empty or not?
Object.keys().length will give you the number of keys in an object. More on reddit.com
๐ŸŒ r/learnjavascript
11
0
March 13, 2022
People also ask

Is null or empty in TypeScript?
In TypeScript, `null` and an empty object are different. `null` is a non-nullish value that represents the intentional absence of an object, whereas `{}` is a valid object. An empty object is not the same as `null`, and TypeScript does not consider them equivalent unless explicitly handled using a union type.
๐ŸŒ
dhiwise.com
dhiwise.com โ€บ blog โ€บ design-converter โ€บ typescript-empty-object-guide-best-practices
Understanding the TypeScript Empty Object
How to make an empty object?
Creating an empty object in TypeScript involves assigning `{}` to a variable. You can define it explicitly using an object type such as `{}` or `Record`. If strict constraints are required, `Record` ensures that no properties are assigned to the object.
๐ŸŒ
dhiwise.com
dhiwise.com โ€บ blog โ€บ design-converter โ€บ typescript-empty-object-guide-best-practices
Understanding the TypeScript Empty Object
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ typescript โ€บ how-to-check-if-an-object-is-empty-in-typescript
How to Check if an Object is Empty in TypeScript ? - GeeksforGeeks
July 23, 2025 - Converting the object into a JSON string allows for easy checking if it's empty by verifying the length of the resulting string. Example: This example shows the use of the above-explained approach.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ typescript-check-if-object-is-empty
Check if an Object is Empty in TypeScript | bobbyhadz
The lodash.isempty method checks if the supplied value is an empty object, collection, Map or Set.
๐ŸŒ
Total TypeScript
totaltypescript.com โ€บ the-empty-object-type-in-typescript
The Empty Object Type in TypeScript | Total TypeScript
April 2, 2024 - Instead of representing an empty object, it represents any value except null and undefined. This is because TypeScript's type system is structural, not nominal.
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/typescript โ€บ utility type to decide if a type is empty or inhabited
r/typescript on Reddit: Utility type to decide if a type is empty or inhabited
June 13, 2024 -

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

๐ŸŒ
Decipher
decipher.dev โ€บ isempty
isEmpty | 30 Seconds of Typescript - Decipher.dev
Returns true if the a value is an empty object, collection, has no enumerable properties or is any type that is not considered a collection. Check if the provided value is null or if its length is equal to 0. ... const isEmpty = (val: any) => ...
๐ŸŒ
DhiWise
dhiwise.com โ€บ blog โ€บ design-converter โ€บ typescript-empty-object-guide-best-practices
Understanding the TypeScript Empty Object
March 3, 2025 - The way TypeScript handles empty objects depends on type rules, object literals, and assignable values.
๐ŸŒ
SPGuides
spguides.com โ€บ typescript-check-if-object-is-empty
How to Check If Object Is Empty in TypeScript?
July 20, 2025 - Configuration defaults: Apply default settings if a config object is empty or incomplete. Check out Convert Number to String with Leading Zeros in TypeScript
๐ŸŒ
Mercury
mercury.com โ€บ blog โ€บ creating-an-emptyobject-type-in-typescript
Creating an EmptyObject type in TypeScript | Mercury
September 19, 2023 - In TypeScript, it is impossible to use the type {} to express โ€œthis object should be empty.โ€ Depending on your experience with TypeScript, you may have been frustrated by this before, or you might be surprised that this is a problem worth ...
๐ŸŒ
Medium
mingyang-li.medium.com โ€บ easiest-way-to-check-for-empty-objects-in-javascript-ab11a004ed57
Easiest Way To Check For Empty Objects In JavaScript | by Mingyang Li | Medium
February 20, 2024 - Easiest Way To Check For Empty Objects In JavaScript Whatโ€™s so difficult about checking if an object is empty? Isnโ€™t it as simple as doing this โ€” const isEmpty = yourObject === {} ? Well, I โ€ฆ
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ typescript-check-if-object-is-empty
TypeScript: Checking if an Object is Empty โ€” xjavascript.com
Checking if an object is empty in TypeScript can be achieved through multiple methods, each with its own advantages and disadvantages. The Object.keys() method is often the most straightforward and performant for general use cases.
๐ŸŒ
xjavascript
xjavascript.com โ€บ blog โ€บ check-if-specific-object-is-empty-in-typescript
TypeScript: How to Check if an Object is Empty (Fixing 'if (object)' Not Working) โ€” xjavascript.com
A common intuition is to use if (object) to determine emptiness, but this rarely works as expected. In JavaScript and TypeScript, objectseven empty onesโ€”are truthy, meaning if ({}) will always evaluate to true.
Top answer
1 of 16
7557

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
2 of 16
1504

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;
}
๐ŸŒ
DEV Community
dev.to โ€บ smpnjn โ€บ how-to-check-if-object-is-empty-in-javascript-5afl
How to Check if Object is Empty in JavaScript - DEV Community
February 5, 2023 - Now we have a function which can tell you if any object is empty, and accounts for both numerable and non-numerable properties. ... Unfortunately this code is a little naive, as Object.keys only checks for enumerable properties. It's perfectly possible for an object to be chock full of non-enumerable properties - making it far from empty.
๐ŸŒ
GitHub
github.com โ€บ bobbyhadz โ€บ typescript-check-if-object-is-empty
GitHub - bobbyhadz/typescript-check-if-object-is-empty: A repository for an article at https://bobbyhadz.com/blog/typescript-check-if-object-is-empty
A repository for an article at https://bobbyhadz.com/blog/typescript-check-if-object-is-empty - bobbyhadz/typescript-check-if-object-is-empty
Author ย  bobbyhadz
๐ŸŒ
Python Guides
pythonguides.com โ€บ check-for-an-empty-object-in-typescript
How To Check For An Empty Object In TypeScript
June 9, 2025 - In this example, we define a User interface and create an empty user object. By checking the length of the array returned by Object.keys(user), we determine that the object is empty.