How to check if an object is empty in JavaScript?
In this article, we are going to see how to check object is empty or not. What are the different ways to check if an object
is empty in JavaScript? So let’s start with our first topic which is what are objects in JavaScript. Do visit Codedam to learn to code and practice what you learned on Playground and create some awesome projects.
What are Objects in JavaScript?
There are two types of data types present in JavaScript. The first one is primitive and the second one is non-primitive. Number, string, booleans, null and undefined have come under primitive data types. Objects and Arrays come under non-primitive data types. The data types which can hold only a single value is called primitive data type. And a data type that can have multiple types of values in a single variable is called a non-primitive data type. We often work with JavaScript objects whenever we need some kind of information, data, and user input. We can use Objects to store information, user-entered data, and many more. And there are so many operations we can do on objects. But we will only see how we can check if an object is empty in JavaScript.
Checking if the Object is empty
finding if a certain object
is empty in JavaScript is one of the most important operations we developers need to do whenever we work with objects in JavaScript. For example, if we are fetching data from a third-party API. It will return us JSON i.e array of objects. And if we show that data even without checking if it is empty or not our program might give us errors.
So we as developers need to think about it. There are many ways to do that but we will only see some most useful and important ways. Let’s start with our first way of checking if an object is empty in JavaScript which is the Object.entries()
method.
With the Object entries function
The Object.entries()
the method takes an object and returns the array of the present key-value pair. And then we can check if the length
of the array
is, and if the length
of the array
is 0 then it means that the passed object does not contain any key-value pair. And hence we can check whether the object is empty or not using the Object.entries()
method. As shown in the following code example.
// check if object is empty.
let ifEmptyObj = {
name: 'Amol shelke',
age: 18,
};
console.log(Object.entries(ifEmptyObj)); //This will be the output.
[ [ 'name', 'Amol shelke' ], [ 'age', 18 ] ]
Code language: JavaScript (javascript)
It will log an array containing the key-value pair of the ifEmptyObj
object
. at this point, the ifEmptyObj
object
is not empty. But now I will make it empty and check if it is empty or not.
ifEmptyObj = {}; // now it is an emtpy object.
if(Object.entries(ifEmptyObj).length === 0) {
console.log('This object is empty');
}; // This object is empty will log on to the console.
Code language: JavaScript (javascript)
Now I made this ifEmptyObj
object empty and I checked if the length of the ifEmptyObj
entries is 0. If it is 0 it means that the object does not contain any key-value pair hence we can find that the object is empty using the Object.entries()
method.
With the Object.keys function
The Object.keys(),
this method is similar to the object.entries()
method but it will not log the key-value pair of the passed object but only the keys. And using that key array we can check if the object is empty or not. As shown in the code example below.
let isEmptyObj2 = {
name: 'empty2',
age: 10,
};
console.log(Object.keys(isEmptyObj2)); // the output will be [ 'name', 'age' ]
Code language: JavaScript (javascript)
In this case, we can do the same we can check if length
of the isEmptyObj2
keys
array
is 0
. And if it is 0
then it means that the passed object is empty. So let’s see this in the code.
isEmptyObj2 = {}; // empty object
if(Object.keys(isEmptyObj2).length === 0) {
console.log('The object is empty');
}; // checking the lenght of the keys array and if the lenght is 0 then it means it is an empty object.
Code language: JavaScript (javascript)
With the Object.values function
Object.values()
this method is the same as the method which we see just now. But when we use the Object.values()
method we will get an array of the values contained in the passed object. So let’s see this in the code.
let isEmptyObj3 = {
name: 'lazy developer',
age: 18,
};
console.log(Object.values(isEmptyObj3)); // The output will be the array containing all the values.['lazy developer', 18]
isEmptyObj3 = {};
if(Object.values(isEmptyObj3).length === 0) console.log('This object is empty');
Code language: JavaScript (javascript)
In this case, now I have to check if the length of the values array is 0 and if it is 0. Then it means that the object is empty.
With the JSON.stringify function
JSON.stringify()
converts the passed Object to a string. And the string also has the length method. So after converting our object to a string we can check if the length of the string is 0. If it is 0 then it means that the Object is empty. So let’s see this method in practice also.
let isEmptyObj4 = {
name: 'Amol shelke',
age: 19,
};
const objToString = JSON.stringify(isEmptyObj4);
console.log(objToString); // The output will be {"name":"Amol shelke","age":19}
console.log(typeof objToString) // string
Code language: JavaScript (javascript)
In the above code example, We convert our isEmptyObj4
object to a string. So now when we log the objToString
variable to the console we get the string containing all our object’s key-value pairs. And we can find that out by checking the type of objToString
variable. If we check the type of the objToString
variable the output will be a string. Let’s check if it is empty or not.
isEmptyObj4 = {};
if(JSON.stringify(isEmptyObj4).lenght === 0 || '{}') {
console.log(isEmptyObj4); // {} will be output but a string type.
console.log('Object is empty');
};
Code language: JavaScript (javascript)
Now after making the isEmptyObj4
object empty. We have to check if it is empty or not. This method is so easy to. Just as we do some operations with string. Same here also, we convert the object to a string and check if the length of the object is 0 or if it is a string with empty curly braces
. If one of the condition become true we will get our expected result. So hence can find if the object is empty or not using the JSON.stringify()
method.
Conclusion
In This article, we learn about what an object in JavaScript is. And also how we can check if an object is empty in JS. We also see the different ways to do that. These are some of the important and most used ways of checking if an object is empty in JS. If Do you have any doubts about JavaScript or do you want to learn about new technologies don’t forget to visit Codedamn.
Sharing is caring
Did you like what Amol Shelke wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: