How to check if JavaScript Object is empty
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.
The best way to check if an object is empty is by using a utility function like the one below.
function isEmpty(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}
So if you have an empty object, you can check whether it is empty by using the above function.
var myObj = {}; // Empty Object
if(isEmpty(myObj)) {
// Object is empty (Would return true in this example)
} else {
// Object is NOT empty
}
Alternatively, you can write the isEmpty function on the Object prototype.
Object.prototype.isEmpty = function() {
for(var key in this) {
if(this.hasOwnProperty(key))
return false;
}
return true;
}
Then you can easily check if the object is empty like so.
var myObj = {
myKey: "Some Value"
}
if(myObj.isEmpty()) {
// Object is empty
} else {
// Object is NOT empty (would return false in this example)
}
Extending the object prototype is not the best thing to do as it can cause some browser issues and other issues with certain frameworks (it's also not always reliable in certain environments). The example I gave is pretty much framework agnostic although.
This is one of those utility functions that will always be useful, especially if you deal with a lot of objects on a daily basis (like I do).
Related protips:
Written by Kyle Ross
Related protips
18 Responses
var isMyObjectEmpty = !Object.keys(myObject).length;
Nice tip. Seems like the Underscore.js implementation -->
http://documentcloud.github.com/underscore/#isEmpty
Just that those guys are checking for Arrays and String in the same function like this:
_.isEmpty = function(obj) {
if (obj == null) return true;
if (_.isArray(obj) || _.isString(obj)) return obj.length === 0;
for (var key in obj) if (_.has(obj, key)) return false;
return true;
};
@avenger7x Yes, that certainly works although it is not supported in some older browsers such as Firefox < 4 and IE < 9. It's a new addition under ECMAScript 5.
@develoser Underscore is just one of those overly useful tools out there and I agree and suggest that others check it out. Thanks for you input!
Hi, I'm wondering why the use of hasOwnProperty is required?
function isEmpty(arg) {
for (var item in arg) {
return false;
}
return true;
}
@denisjacquemin: historically, for(x in y) will show "toString" as a value of "x". using hasOwnProperty allows for skipping over "built in" or "metadata" properties, and just evaluate "value" properties.
If you don't want to copy-paste this all over your different repositories, you can also try this NPM package or this Bit component.
They should work just fine and make it useable anywhere you need.
const isEmpty = function (input) {
if (typeof input === 'array') {
return input.length === 0;
}
return !input || Object.keys(input).length === 0;
}
JSON.stringify(myObj) == "{}"
The Object.prototype.isEmpty approach shouldn't be used.
I used that with jQuery and it made $.ajax failed.
Took me more than an hour to solve it.
if(myObj.hasOwnProperty(key)) {
// true;
} else {
// false
}
it doesn't work if it comes:
let obj2 = {
[Symbol('name')]: 'alice'
}
let obj3 = Object.defineProperty({}, 'name', {
value: 'alice',
enumerable: false
})
i think we can use this:
const isEmptyObj = object => !Object.getOwnPropertySymbols(object).length && !Object.getOwnPropertyNames(object).length
Why not to use _.isEmpty()
from the lodash library?
thanks! I struggled with this for a very long time.
A lot of cool ways have been shown here. Thanks for the advice ;)
Thanks for the many ways to overcome the problem. It really came in handy.
Thx for imformation
How about
function isEmpty(obj){
return Object.keys(obj).length == 0
}
?