Languages
[Edit]
EN

JavaScript - how to check if object is empty?

7 points
Created by:
Mariam-Barron
781

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

 

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join