How to Check if an Object is Empty in JavaScript

The length property is used to check the number of keys. Like underscore’s and lodash’s _.isObject(), this is not exclusively for objects but also applies to arrays and strings. Another alternative is to use is.js as opposed to jquery , lodash , or underscore (16.4kB). Is.js proved to be the fastest library among aforementioned libraries that could be used to determine whether an object is empty. Object.keys will return an Array, which contains the property names of the object.

check if object is empty javascript

So just using the Object.keys, it does return true when the object is empty ✅. But what happens when we create a new object instance using these other constructors.

A Bullet-Proof Solution

Let’s discuss and explain each of them separately. Libraries are common, typically fast/optimized, and some are present in many projects due to how useful they are. Several of them can also be used for checking whether an object is empty or not. The majority of them offer excellent compatibility for older browsers. Statement will loop through the enumerable property of the object.

check if object is empty javascript

With JavaScript, it can be difficult to check whether an object is empty. With Arrays, you can easily check with myArray.length, but on the other hand, objects do not work that way. Get the keys of the given object and, if there are none, the object must be empty. Pick whatever method works best for you and your codebase and, as always, feel free to leave a comment if you find this useful or have any suggestions on a better way to do this. Let’s create a function using some of JavaScript’s newer features to safely check if an object is empty. Libraries help us write code faster, by incorporating advanced functionality, used and honed by countless other developers, instead of writing our own solutions. If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.

Loop Over Object Properties With for…in

Depends if you consider a date being always “full” despite never exposing keys. But I agree that if that’s your plan, adding some supplementary instanceof check for Date constructor is a good option. It is not currently accepting new answers or interactions. Once again, this method will fail on a null or undefined input. In this quick article, I’ll show you how you can check if an object is empty in JavaScript. In this article, we have taken a look at how to check whether an object is empty or not in JavaScript. To this end – we’ve used several Vanilla JS approaches, jQuery, Underscore, Lodash, Ramda, Hoek and the JSON module.

  • Most of the time, these will be equivalent, but there is a risk that Object.keys() will miss certain properties that have been declared not to be enumerable.
  • But I agree that if that’s your plan, adding some supplementary instanceof check for Date constructor is a good option.
  • @Damien, tbf the question is 11 years old & this answer was posted 2 weeks ago.
  • I’m a passionate and driven web developer with an eye for design and appreciation for the human aspect of technology.

The second method is looping through the object using object.hasOwnProperty. When the object contains the “key” property using the object.hasOwnProperty() method, a function is created. This would return true if it could find no keys in the loop, which means that the object is empty. If any key is found, the loop breaks returning false.

# Community Input

Object.keys() returns the array whose elements are strings that correspond to the enumerable properties found directly upon the object. The property order is similar to that given by the object manually in a loop applied to the properties.

How do you check if an array is empty or null?

To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.

Connect and share knowledge within a single location that is structured and easy to search.

How to Check If an Object Is Empty in JavaScript

When it comes to small applications that don’t require external dependencies – checking whether an object is empty is best done with pure JavaScript. However, if your application already has external libraries such as lodash and underscore – they offer great ways to perform these checks as well.

  • This way I don’t need to use those expensive methods to check if an object is empty.
  • Even when it does work, toSource() is a horrible way to do this (as is JSON.encode()).
  • Then you can easily check if the object is empty like so.
  • Apart from that, if you’re using a JavaScript utility library in your project, there’s a chance that it already provides a built-in method to check if an object is empty.
  • Operator to effectively change it into a boolean.

Article was published on: 10/7/22

Author: Viktor Nikolaev

Victor is a professional crypto investor and stockbroker, specializing in such areas as trading on the stock exchange, cryptov currencies, forex, stocks and bonds. In this blog he shares the secrets of trading, current currency indices, crypt currency rates and tells about the best forex brokers. If you have any questions, you can always contact

Leave a Reply