You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

Copyfunction 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:

Copyfunction 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:

Copyfunction 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:

CopyjQuery.isEmptyObject({}); // true

lodash:

Copy_.isEmpty({}); // true

Underscore:

Copy_.isEmpty({}); // true

Hoek:

CopyHoek.deepEqual({}, {}); // true

ExtJS:

CopyExt.Object.isEmpty({}); // true

AngularJS (version 1):

Copyangular.equals({}, {}); // true

Ramda:

CopyR.isEmpty({}); // true
Top answer
1 of 16
7555

You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

Copyfunction 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:

Copyfunction 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:

Copyfunction 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:

CopyjQuery.isEmptyObject({}); // true

lodash:

Copy_.isEmpty({}); // true

Underscore:

Copy_.isEmpty({}); // true

Hoek:

CopyHoek.deepEqual({}, {}); // true

ExtJS:

CopyExt.Object.isEmpty({}); // true

AngularJS (version 1):

Copyangular.equals({}, {}); // true

Ramda:

CopyR.isEmpty({}); // true
2 of 16
1504

If ECMAScript 5 support is available, you can use Object.keys():

Copyfunction 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:

Copyfunction isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
🌐
Reddit
reddit.com › r/javascript › i often find myself writing object.keys(someobject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
r/javascript on Reddit: I often find myself writing Object.keys(someObject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
September 4, 2018 -

Hi everyone,

I very often find myself writing something like

if( Object.keys(someObject).length > 0 ) {

//do some wild stuff

}

To check if a basic object is empty or not i.e. not {} there must be a beautiful way.

I know lodash and jQuery have their solutions, but I don't want to import libraries for a single method, so I'm about to write a function to use across a whole project, but before I do that I want to know I'm not doing something really stupid that ES6/ES7/ES8 can do that I'm just not aware of.

edit solution courtesy of u/vestedfox

Import https://www.npmjs.com/package/lodash.isequal

Total weight added to project after compilation: 355 bytes

Steps to achieve this.

npm i --save lodash.isequal

then somewhere in your code

const isEqual = require('lodash.isequal');

If you're using VueJS and don't want to have to include this in every component and don't want to pollute your global namespace you can do this in app.js

const isEqual = require('lodash.isequal');
Vue.mixin({
  methods: {
    isEqual: isEqual
  }
});

Then in your components you can simply write.

if( this.isEqual(someObject, {})) {
   console.log('This object has properties');
}

Discussions

I often find myself writing Object.keys(someObject) > 0 to test if an object isn't {} (empty) there must be a more beautiful way.
const isEmpty = obj => Object.keys(obj).length === 0; ​ isEmpty({}); // true More on reddit.com
🌐 r/javascript
48
25
September 4, 2018
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
Checking if property in object is an empty string
That's not how a switch statement works. You switch on an expression and the cases are specific values it can have. See MDN . You should just use an if/else statement for this. More on reddit.com
🌐 r/learnjavascript
2
1
May 24, 2022
What is the best method to check if a variable is not null or empty?
It depends what you mean by empty, or how strict you want to be. These values will coerce to false: undefined null '' (empty string) 0 NaN Everything else coerces to true. So, if you are OK with rejecting all of those values, you can do: if(PostCodeInformation) { } If you want to make sure that PostCodeInformation is really an object value (and not a number or boolean, etc): if(typeof PostCodeInformation === 'object' && PostCodeInformation !== null) { } You have to do the null-check there, because in JavaScript typeof null returns 'object'. So dumb. If you want to make sure that PostCodeInformation has some property that you really need: if(PostCodeInformation && PostCodeInformation.myCoolProperty) { } Etc, etc More on reddit.com
🌐 r/javascript
18
3
August 2, 2015
🌐
DEV Community
dev.to › onlinemsr › 7-easy-ways-to-check-if-an-object-is-empty-in-javascript-ddm
7 Easy Ways To Check If An Object Is Empty In JavaScript - DEV Community
July 5, 2023 - In this post, let’s look at four different methods that you can use to check if an object is empty. The JavaScript Object.keys() method returns an array of a given object’s property names.
🌐
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.
🌐
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 - Lodash is a modern JavaScript utility library that can perform many JavaScript functionalities with very basic syntax. For example, if you want to check if an object is empty, you only need to use the "isEmpty" method.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › some
Array.prototype.some() - JavaScript | MDN
3 days ago - If such an element is found, some() immediately returns true and stops iterating through the array. Otherwise, if callbackFn returns a falsy value for all elements, some() returns false. Read the iterative methods section for more information about how these methods work in general. some() acts like the "there exists" quantifier in mathematics. In particular, for an empty array, it returns false for any condition.
🌐
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
If the length of the array is 0, then we know that the object is empty. function isEmpty(obj) { return **Object.keys(obj).length === 0**; } We can also check this using Object.values and Object.entries.
Find elsewhere
🌐
Oprea
oprea.rocks › blog › is-javascript-object-empty.html
Adrian OPREA | Remote Full Stack Software Engineer | How to check if a JavaScript object is empty
January 18, 2019 - The code calls a smart contract getter, using Web3.js, retrieves whatever the return value is, and returns its plain text representation. Unfortunately, if there is no element at the specified index the getter throws an error, which is … an empty object.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-an-object-is-empty-using-javascript
How to Check an Object is Empty using JavaScript? - GeeksforGeeks
July 11, 2025 - If the length of array is 0, then object is empty. JavaScript · let obj = {}; if (Object.keys(obj).length === 0) { console.log("Empty Object") } else { console.log("Not Empty Object") } Output ·
🌐
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 - The safest way to check if an object is empty in JavaScript is to convert it into a string and check if it equals to a plan "{}" string — if you’re not using a library.
🌐
Zipy
zipy.ai › blog › how-do-i-test-for-an-empty-javascript-object
how do i test for an empty javascript object
April 12, 2024 - Throughout this article, we'll explore various techniques for checking if an object is empty in JavaScript. We'll cover built-in methods, custom functions, and best practices to ensure efficient and effective object handling.
🌐
SamanthaMing
samanthaming.com › tidbits › 94-how-to-check-if-object-is-empty
How to Check if Object is Empty in JavaScript | SamanthaMing.com
const empty = {}; /* ------------------------- Plain JS for Newer Browser ----------------------------*/ Object.keys(empty).length === 0 && empty.constructor === Object // true /* ------------------------- Lodash for Older Browser ----------------------------*/ _.isEmpty(empty) // true ... A. Empty Object Check in Newer Browsers ... B. Empty Object Check in Older Browsers ... Vanilla JavaScript is not a new framework or library.
🌐
Bonsaiilabs
bonsaiilabs.com › check-empty-object
How to check if a JavaScript Object is Empty - bonsaiilabs
We can use it by calling object.entries and pass it as an argument users whose key value pairs are to be returned. And since it returns an array, we can check if the length of the array is zero, it means that object doesn't contain anything. We can now log a message - users is empty, if the condition is true.
🌐
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 - const obj1: Record<string, any> = {}; const obj2: Record<string, any> = { name: "John", age: 30 }; if (Object.keys(obj1).length === 0) { console.log('obj1 is empty'); } else { console.log('obj1 is not empty'); } if (Object.keys(obj2).length === 0) { console.log('obj2 is empty'); } else { console.log('obj2 is not empty'); } ... This approach utilizes the Object.entries() method to get an array of key-value pairs from the object and then checks the length of the array.
🌐
Flexiple
flexiple.com › javascript › check-if-object-is-empty
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent - Flexiple
An object in JavaScript is considered empty if it has no enumerable properties. One straightforward method to check this condition involves using a for…in loop, which iterates over enumerable property names of an object.
🌐
The Valley of Code
thevalleyofcode.com › how-to-check-object-empty
How to check if an object is empty in JavaScript
If it returns an empty array, it means the object does not have any enumerable property, which in turn means it is empty. ... You should also make sure the object is actually an object, by checking its constructor is the Object object:
🌐
HackerNoon
hackernoon.com › how-to-determine-whether-a-javascript-object-is-empty
How to Determine Whether a JavaScript Object Is Empty | HackerNoon
February 8, 2023 - Defining a new object in Javascript is pretty easy - but what if you want to find out if it's empty?
🌐
Leapcell
leapcell.io › blog › how-to-check-if-an-object-is-empty-in-javascript
How to Check if an Object Is Empty in JavaScript | Leapcell
July 25, 2025 - Be careful to check that the value is actually an object and not null, which is also technically an object in JavaScript: function isEmpty(obj) { return obj && typeof obj === "object" && Object.keys(obj).length === 0; } The most reliable and ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
The optional chaining (?.) operator accesses an object's property or calls a function. If the object accessed or function called using this operator is undefined or null, the expression short circuits and evaluates to undefined instead of throwing an error.
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-check-object-is-empty-or-not › td-p › 2943062
How to check Object is Empty Or not - ServiceNow Community
June 20, 2024 - Hi All, Can you please suggest me how to check Object is empty or not, If JSON is empty then complier should not go till " return JSON.stringify(arr);" if JSON is not empty then complier should touch return JSON.stringify(arr); line in Code. issue: script include returning error message like "Em...