A javascript object is a collection of properties in key-value pairs. An empty object means an object which does not contain any property.
To know about javascript objects, refer this post.
Before accessing a property of an object, it is recommended to check for object being empty to avoid run time errors.
There are many methods to test empty object in javascript and this post will discuss them with examples.
Iterate over the properties of object which needs to be checked for being empty using a
for
loop. In every iteration, check if the property belongs to this object using hasOwnProperty
.This function takes a property name as argument and returns
true
if the property belongs to the object and false
if it the property does not belong to the object. Example,
let obj = {'name': 'codippa'};
let isEmpty = true;
// iterate over object properties
for(let property in obj){
// check if the property belongs to this object
if(obj.hasOwnProperty(property)){
isEmpty = false;
}
}
console.log(isEmpty); // prints false
JSON.stringify
method takes an object as argument and returns the object in string format. Compare the string representation of the object with the string representation of an empty object.If they both are same, then the object which is to be tested is also empty. Example,
let obj = {'name': 'codippa'};
let emptyObj = {};
// compare the stringified representations
let isEmpty = JSON.stringify(obj) == JSON.stringify(emptyObj);
console.log(isEmpty); // prints false
Method 3: Using Object.keys method
Object.keys
method accepts an object as argument and returns an array containing the keys of all the properties of an object.
Checking the length of this array will give a hint of properties in array. If the length is 0, then there are no keys in the object which means that the object is empty.
length
property.Example,
let obj = {'name': 'codippa'};
// print all the keys
console.log(Object.keys(obj)); // prints ["name"]
// compare the length of keys with 0
let isEmpty = Object.keys(obj).length == 0;
console.log(isEmpty); // prints false
Object.entries
function accepts an object as argument and returns an array of arrays of properties of object. Array of arrays of properties means that the function returns an array whose every element is an array which contains the key and value pair of the object property. Example,
let obj = {'name': 'codippa', 'type': 'website'};
// print all the entries
console.log(Object.entries(obj));
Above example prints following output
0: [“name”, “codippa”] 1: [“type”, “website”]
As evident from the output of Object.entries
function, it returns an array of object properties. Now in order to test for empty object, length of this array can be compared with 0.
If it is 0, then the object contains no properties meaning it is empty. Example,
let obj = {'name': 'codippa'};
// compare the length of keys with 0
let isEmpty = Object.entries(obj).length == 0;
console.log(isEmpty); // prints false
This method was introduced in ECMAScript5+ and hence it is supported by below browser versions.
Chrome 54
Firefox 47
IE Not supported
Edge 14
Safari 10.1
Method 5: Using Object.getOwnPropertyNames
Object.getOwnPropertyNames
function accepts an object as argument and returns an array of the keys of all the property names of the object.
If you check the length of the returned array and compare it with 0, then empty object can be checked. Example,
let obj = {'name': 'codippa'};
// compare the length of keys with 0
let isEmpty = Object.getOwnPropertyNames(obj).length == 0;
console.log(isEmpty); // prints false
Object.getOwnPropertyNames
was introduced in ECMAScript5.
Hope this article was helpful for you. Please hit the clap icon below to like.