Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check if an object is empty using JavaScript?
In JavaScript, checking if an object is empty is a common requirement when working with dynamic data. An empty object contains no properties, and attempting operations on assumed non-empty objects can lead to unexpected behavior.
For example, when fetching data from an API, you might receive an empty object if no results are found. Before processing this data, it's essential to verify whether the object contains any properties.
We will explore three reliable methods to check if an object is empty in JavaScript.
Using Object.keys() Method
The Object.keys() method returns an array of all enumerable property names of an object. If this array has a length of 0, the object is empty.
Syntax
let isEmpty = Object.keys(obj).length === 0;
if (isEmpty) {
// object is empty
} else {
// object has properties
}
Example
<html>
<body>
<h3>Using Object.keys() method to check if object is empty</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
let obj1 = {
prop1: 10,
prop2: "Hello"
};
let obj2 = {};
// Check first object
let obj1Empty = Object.keys(obj1).length === 0;
if (obj1Empty) {
output.innerHTML += "obj1 is empty<br>";
} else {
output.innerHTML += "obj1 contains: " + JSON.stringify(obj1) + "<br>";
}
// Check second object
let obj2Empty = Object.keys(obj2).length === 0;
if (obj2Empty) {
output.innerHTML += "obj2 is empty<br>";
} else {
output.innerHTML += "obj2 contains: " + JSON.stringify(obj2) + "<br>";
}
</script>
</body>
</html>
obj1 contains: {"prop1":10,"prop2":"Hello"}
obj2 is empty
Using for...in Loop
The for...in loop iterates over all enumerable properties of an object. If the loop executes even once, the object is not empty.
Syntax
function isObjectEmpty(obj) {
for (let key in obj) {
return false; // object has at least one property
}
return true; // no properties found
}
Example
<html>
<body>
<h3>Using for...in loop to check if object is empty</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
let obj1 = { prop1: false };
let obj2 = {};
function isObjectEmpty(obj) {
for (let key in obj) {
return false; // Found a property
}
return true; // No properties found
}
// Test both objects
if (isObjectEmpty(obj1)) {
output.innerHTML += "obj1 is empty<br>";
} else {
output.innerHTML += "obj1 is not empty: " + JSON.stringify(obj1) + "<br>";
}
if (isObjectEmpty(obj2)) {
output.innerHTML += "obj2 is empty<br>";
} else {
output.innerHTML += "obj2 is not empty: " + JSON.stringify(obj2) + "<br>";
}
</script>
</body>
</html>
obj1 is not empty: {"prop1":false}
obj2 is empty
Using JSON.stringify() Method
The JSON.stringify() method converts an object to a JSON string. An empty object always converts to "{}", so we can compare the result with this string.
Syntax
if (JSON.stringify(obj) === "{}") {
// object is empty
} else {
// object has properties
}
Example
<html>
<body>
<h3>Using JSON.stringify() method to check if object is empty</h3>
<p id="output"></p>
<script>
let output = document.getElementById("output");
let education = {
totalYears: 12,
school: "Online"
};
let emptyObj = {};
// Check education object
if (JSON.stringify(education) === "{}") {
output.innerHTML += "Education object is empty<br>";
} else {
output.innerHTML += "Education object is not empty: " + JSON.stringify(education) + "<br>";
}
// Check empty object
if (JSON.stringify(emptyObj) === "{}") {
output.innerHTML += "emptyObj is empty<br>";
} else {
output.innerHTML += "emptyObj is not empty<br>";
}
</script>
</body>
</html>
Education object is not empty: {"totalYears":12,"school":"Online"}
emptyObj is empty
Comparison
| Method | Performance | Readability | Best For |
|---|---|---|---|
| Object.keys() | Fast | High | Most cases |
| for...in loop | Fastest | Medium | Large objects |
| JSON.stringify() | Slowest | High | Simple objects |
Conclusion
The Object.keys() method is generally the best choice for checking empty objects due to its balance of performance and readability. Use for...in loop for performance-critical scenarios with large objects.
