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

Output : 3

Source: Object.keys()

Answer from DeepSea on Stack Overflow
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
Tutorials for Software Developers on Built InCreate React App and TypeScript — A Quick How-To · If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.
🌐
CoreUI
coreui.io › answers › how-to-check-if-an-object-is-empty-in-javascript
How to check if an object is empty in JavaScript · CoreUI
October 23, 2025 - Here Object.keys(obj) returns an array of all enumerable property names from the object. If the array length equals 0, the object has no properties and is considered empty.
Discussions

Check if specific object is empty in typescript
1 How to check object property empty value in typescript · 5 Check if an object's properties are null in Typescript More on stackoverflow.com
🌐 stackoverflow.com
Checking if a state object is empty - javascript
I have a react js state object and would like to execute some code if the object is empty. Is there something wrong with my logic because the code inside the if block is not getting executed. if ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
People also ask

What is the fastest way to check if an object is empty in React?
The fastest way is to use `Object.keys()`, which checks the object’s properties efficiently.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
Why should I use Lodash's isEmpty method in my React project?
Lodash’s isEmpty method is reliable across different types and browsers, and it simplifies code readability.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
Yes, `JSON.stringify()` can be slower for large objects due to the string conversion process.
🌐
dhiwise.com
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
🌐
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.
🌐
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 TypeScript, determining whether an object is empty involves checking if the object has any properties. This can be achieved through various built-in methods and loops.
🌐
DhiWise
dhiwise.com › post › react-check-if-object-is-empty-a-simple-guide-for-developers
React Check if Object is Empty: A Simple Guide
November 6, 2024 - Object.keys() offers a straightforward way to check if an object is empty in JavaScript, making it particularly useful in React applications.
Find elsewhere
🌐
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.
🌐
Flexiple
flexiple.com › javascript › javascript-object-empty-check-guide
How to Check if an Object is Empty in JavaScript - Flexiple
May 6, 2024 - Alternatively, developers can leverage JSON.stringify() to convert the object into a JSON string and then check if it represents an empty object ('{}'). function isEmptyObject(obj) { return JSON.stringify(obj) === '{}'; } ... const emptyObject ...
🌐
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 - Checking if the argument equals to a pair of empty {} after you convert it into a string is, by far, the safest and the easiest way to ensure whether a piece of data is an empty object.
🌐
Bobby Hadz
bobbyhadz.com › blog › react-check-if-object-is-empty
How to check if an Object is Empty in React | bobbyhadz
If the array of keys has a length of 0, then the object is empty. ... Copied!import {useEffect, useState} from 'react'; export default function App() { const [person, setPerson] = useState({}); useEffect(() => { if (Object.keys(person).length ...
🌐
Fjolt
fjolt.com › article › javascript-check-if-object-empty
How to Check if Object is Empty in JavaScript
February 4, 2023 - Object.defineProperty(obj, 'someProp', { value: "non-enumerable property", writable: true }) let isObjEmpty = (obj) => { return Object.getOwnPropertyNames(obj).length === 0 && obj.constructor === Object } console.log(isObjEmpty(empty)); // Returns false · Now we have a function which can tell you if any object is empty, and accounts for both numerable and non-numerable properties.
🌐
freeCodeCamp
freecodecamp.org › news › check-if-an-object-is-empty-in-javascript
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent
November 7, 2024 - This will return either true or false. If the object is empty, it will return true, otherwise, it will return false.
🌐
xjavascript
xjavascript.com › blog › checking-if-a-state-object-is-empty
How to Check if a React State Object is Empty: Troubleshooting Why Your If Block Isn't Executing — xjavascript.com
If you check for empty objects frequently, define a helper like isEmptyObject to avoid repetition and enforce consistency. It’s unreliable and inefficient—stick to Object.keys or Lodash.
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;
}
🌐
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

🌐
Upmostly
upmostly.com › home › tutorials › how to check if object is empty in javascript
How to Check if an Object is Empty in JavaScript (Code Examples)
October 28, 2021 - In our case, the array for the person object will be empty, which is why we then check the length of the array. Simple, effective, concise. Let’s see it in action! const person = {} if (Object.keys(person).length === 0) { // is empty } else { // is not empty }
🌐
Sentry
sentry.io › sentry answers › javascript › how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
December 15, 2022 - This method was used as an alternative to using Object.keys before it was added to JavaScript in the 2011 ECMAScript 5 specification and is widely supported by browsers. You can use JSON.stringify() to convert the value to a JSON string to check if the value is an empty object.