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
Top answer
1 of 16
7554

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
1500

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/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
49
25
September 4, 2018
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
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
January 31, 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
August 16, 2022
🌐
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.
🌐
Flexiple
flexiple.com › javascript › check-if-object-is-empty
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent - Flexiple
May 8, 2024 - 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.
🌐
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.
🌐
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?
Find elsewhere
🌐
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.
🌐
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 ...
🌐
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.
🌐
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...
🌐
Futurestud.io
futurestud.io › tutorials › how-to-check-if-an-object-is-empty-in-javascript-or-node-js
How to Check if an Object is Empty in JavaScript or Node.js
November 12, 2020 - JavaScript provides the Object.keys() method returning the array of keys from the given object. You can leverage this method detecting whether the number of keys is zero which tells you a given object is empty: const user = {} const isEmpty = Object.keys(user).length === 0 · You may also create ...
🌐
W3Schools
w3schools.com › js › js_typeof.asp
JavaScript typeof
An empty string has both a legal value and a type. ... In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object.
🌐
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.
🌐
Scaler
scaler.com › topics › check-if-object-is-empty-javascript
How to Check if an Object is Empty in JavaScript - Scaler Topics
January 5, 2024 - We can use Object.entries() similar to how we used methods Object.getOwnPropertNames() and Object.values() to evaluate whether an object is empty. Take a look at the example mentioned below.
🌐
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
July 22, 2024 - 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.
🌐
Tomekkolasa
tomekkolasa.com › how-to-check-if-object-is-empty-in-javascript
How to check if an object is empty in JavaScript | Tomek Kolasa | Deep dive into a full-stack JavaScript
July 10, 2020 - On each iteration we call the object’s .hasOwnProperty() method. We use it to check if the property actually belongs to this object and not to one of its ancestors up the prototype chain.
🌐
Favtutor
favtutor.com › articles › check-javascript-object-empty
Check if an JavaScript Object is Empty (5 Easy Methods)
January 22, 2024 - Then we check the length of the the array. If the length of the array comes out to be zero, the object is empty otherwise it is not empty. Thus, we have obtained the desired result.
🌐
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.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Check if an Object Is Empty in JavaScript | Envato Tuts+
April 29, 2022 - The JSON.stringify method is used to convert a JavaScript object to a JSON string. So we can use it to convert an object to a string, and we can compare the result with {} to check if the given object is empty.