EN
JavaScript - how to check if object is empty?
7
points
Using JavaScript it is possible to check if object is empty in following way.
1. Custom function example
// ONLINE-RUNNER:browser;
function isEmpty(object) {
for (var name in object) {
if (object.hasOwnProperty(name)) {
return false;
}
}
return true;
}
// Usage example:
var object1 = { };
var object2 = {
items: [ ]
};
var object3 = {
result: false
};
var object4 = {
ptint: function() { /* nothing here... */ }
};
console.log(isEmpty(object1)); // true
console.log(isEmpty(object2)); // false
console.log(isEmpty(object3)); // false
console.log(isEmpty(object4)); // false