. Advertisement .
..3..
. Advertisement .
..4..
In Javascript, objects store a collection of properties, including a name and a value. Checking if an object is empty in Javascript is a basic and frequent operation. Now let’s explore several methods to accomplish this task.
How To Check If An Object Is Empty In Javascript
Method 1: Use The Object.keys()
The static Object.keys() method returns an array, containing the property names. You can check to see whether the length of this array is 0 or higher.
If there is no key present, the object is empty:
Object.keys(obj).length === 0 && obj.constructor === Object;
This can be also a reusable function to check multiple times in the project:
const isEmptyObject = (obj) => {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
console.log(isEmptyObject(emptyObject)); // true
The object.values() and object.entries() methods can also be employed in the same way as the Object.keys().
Method 2: Use The Object.values()
An object with no values means it is empty, undefined, or null:
const isEmptyObject = (obj) => {
return Object.values(obj).length === 0 && obj.constructor === Object;
}
console.log(isEmptyObject(emptyObject)); // true
Method 3: Use The Object.entries()
This method represents the key-value pairs to be used as a wrapper:
const isEmptyObject = (obj) => {
return Object.entries(obj).length === 0 && obj.constructor === Object;
}
console.log(isEmptyObject(emptyObject)); // true
Method 4: use JSON.stringify
This is the simplest method you can choose. When using this one, the output is an opening and a closing bracket:
JSON.stringify(objectName) === '{}';
You can be wrapped into the following function:
const isEmptyObject = (objectName) => {
return JSON.stringify(objectName) === '{}';
}
console.log(isEmptyObject(emptyObject)); // true
Method 5: Use Underscore And Lodash
Both are utility libraries, imported as _ and via a CDN:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/underscore-umd-min.js"></script>
They have the same syntax for checking the value of an object:
_.isEmpty();
This works with all data structures, including strings, lists, objects, and arrays. It is a wrapper checking the object’s length. The result will be true or false:
_.isEmpty(emptyObject); // true
Method 6: Use jQuery
jQuery is a common Javascript library, which is present in various projects. It is stable thanks to the lightweight and built-in features. jQuery can be imported:
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
Syntax:
$ npm install jquery
Here is how you can use it to check the object:
$.isEmptyObject(emptyObject); // true
Method 7: Use Ramda
This functional Javascript library can be imported via:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js"></script>
The function offers an isEmpty() function:
R.isEmpty(emptyObject); // true
Conclusion
This article has introduced you to a new look at how to check if an object is empty in Javascript. There are various methods you can choose from and they are suitable for different cases.
Leave a comment