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.
More Tips and Tricks for Javascript
- How to get the current URL with Javascript
- Javascript Objects Cheatsheet
- How Generator Functions work in Javascript
- Import, Export and Require in Javascript
- What is Nullish Coalescing (or ??) in Javascript
- How does the Javascript logical AND (&&) operator work?
- Truthy and Falsy Values in Javascript
- Javascript Shallow Copies - what is a Shallow Copy?
- Javascript Operators and Expressions
- Javascript Map, and How it is Different from forEach