28
How to check if an object is empty in JavaScript?
To check if an object is empty, we can use the keys()
method available in the global Object
object and then use the length
property in the array
returned from the method in JavaScript.
// empty object
const emptyObj = {};
// get the number of keys
// present in the object
// using the Object.keys() method
// then use the length property to get the
// number of keys present
const numberOfKeysPresent = Object.keys(emptyObj).length;
// check using an if statement
// add emptyObj to check for null and undefined values
if (emptyObj && numberOfKeysPresent) {
console.log("Object is not empty since it has 1 or more key in it.");
} else {
console.log("Object is empty.");
}
For example, let's say we have an empty object like this,
// empty object
const emptyObj = {};
Now to check if the emptyObj
object is empty, we can use the Object.keys()
method and then use the length
property in the array
retuned from the method like this,
// empty object
const emptyObj = {};
// get the number of keys
// present in the object
// using the Object.keys() method
// then use the length property to get the
// number of keys present
const numberOfKeysPresent = Object.keys(emptyObj).length;
The Object.keys()
accepts
- an
object
as an argument to the method - and returns an
array
of keys present in the object
After getting the length or number of keys present in the emptyObj
object, we can say that:
- if the value is number
0
, then it is empty - and if the value is greater than
0
, then the object is not empty since it has 1 or more keys present in it.
So let's check this using an if
statement like this,
// empty object
const emptyObj = {};
// get the number of keys
// present in the object
// using the Object.keys() method
// then use the length property to get the
// number of keys present
const numberOfKeysPresent = Object.keys(emptyObj).length;
// check using an if statement
if (numberOfKeysPresent) {
console.log("Object is not empty since it has 1 or more key in it.");
} else {
console.log("Object is empty.");
}
To check for null
and undefined
values, we can also check to see if the object is present like this,
// empty object
const emptyObj = {};
// get the number of keys
// present in the object
// using the Object.keys() method
// then use the length property to get the
// number of keys present
const numberOfKeysPresent = Object.keys(emptyObj).length;
// check using an if statement
// add emptyObj to check for null and undefined values
if (emptyObj && numberOfKeysPresent) {
console.log("Object is not empty since it has 1 or more key in it.");
} else {
console.log("Object is empty.");
}
See the above code live in JSBin
We can also do the same using a conditional (ternary) operator
like this,
// empty object
const emptyObj = {};
// get the number of keys
// present in the object
// using the Object.keys() method
// then use the length property to get the
// number of keys present
const numberOfKeysPresent = Object.keys(emptyObj).length;
// check using conditional (ternary) operator
emptyObj && numberOfKeysPresent
? console.log("Object is not empty since it has 1 or more key in it.")
: console.log("Object is empty.");
See the above code live in JSBin
That's all π!
28