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.
Top comments (3)
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:Your
isObjEmpty
function is probably fine for most simple use cases, but I think it is important to highlight this point.If enumerability is likely to be an issue, you simply need to use
Object.getOwnPropertyNames
instead ofObject.key
- but I'll update the code so others benefit from that information.What about
JSON.stringify(empty) === '{}'