Languages
[Edit]
EN

JavaScript - how to check if object is empty?

7 points
Created by:
Kate_C
19790

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

 

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join