Javascript

How to Check if Object is Empty in JavaScript

📣 Sponsor

Defining a new object in Javascript is pretty easy - but what if you want to find out if it’s empty? For example, {} is an empty object, but how do we actually test that this is the case?

let myObject = {}

The easiest (and best) way to do this, is to use Object.keys(). This method turns all the keys in an object to an array, which we can then test the length of:

let myObject = {} console.log(Object.keys(myObject).length) // Returns 0!

But wait… Javascript is well known for how it handles types strangely - and new constructors return an object with length 0:

let myFunction = function() { console.log("hello") } console.log(Object.keys(new myFunction()).length)

Fortunately, we can check if something is an object by checking its constructor property:

console.log(function myFunction() {}.constructor) // Function console.log({}.constructor) // Object

Therefore, we can check if an object is empty if its constructor is an Object, and it has an Object.keys() value of 0:

let empty = {} let isObjEmpty = (obj) => { return Object.keys(obj).length === 0 && obj.constructor === Object } console.log(isObjEmpty(empty)); // Returns true, Object is empty!

This will not work if some keys are non-enumerable, though. Depending on your use case, this might be fine - but let’s look at how to check for non-enumerable properties next.

Including Non-Enumerable keys

It is possible to define keys on an Object which are non-enumerable. That means they won’t show up in for loops or in Object.keys.

Fortunately, we can use Object.getOwnPropertyNames to get all non-enumerable and enumerable keys on an object. Therefore, to check if an object is empty and check for both enumerable and non-enumerable keys, we only have to change Object.keys to Object.getOwnPropertyNames:

let empty = {} // Example: Add a non-enumerable property to our object using defineProperty. All properties added with defineProperty are non-enumerable by default. 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.

Last Updated 1675542718321

More Tips and Tricks for Javascript

Subscribe for Weekly Dev Tips

Subscribe to our weekly newsletter, to stay up to date with our latest web development and software engineering posts via email. You can opt out at any time.

Not a valid email