DEV Community

Cover image for How to Check if Object is Empty in JavaScript
Johnny Simpson
Johnny Simpson

Posted on • Originally published at fjolt.com

How to Check if Object is Empty in JavaScript

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 = {}
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

console.log(function myFunction() {}.constructor) // Function
console.log({}.constructor) // Object
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Now we have a function which can tell you if any object is empty, and accounts for both numerable and non-numerable properties.

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ • Edited

Unfortunately this code is a little naive, as Object.keys only checks for enumerable properties. It's perfectly possible for an object to be chock full of non-enumerable properties - making it far from empty. Consider the following code:

const obj = {}

Object.defineProperty(obj, 'myProperty', {
  value: 42,
  writable: true
})

console.log(obj.myProperty)  // 42

// using your 'isObjEmpty' function
console.log(isObjEmpty(ob))  // true
Enter fullscreen mode Exit fullscreen mode

Your isObjEmpty function is probably fine for most simple use cases, but I think it is important to highlight this point.

Collapse
 
smpnjn profile image
Johnny Simpson

If enumerability is likely to be an issue, you simply need to use Object.getOwnPropertyNames instead of Object.key - but I'll update the code so others benefit from that information.

Collapse
 
sabinbaniya profile image
Sabin Baniya

What about JSON.stringify(empty) === '{}'