Code snippets to help you check whether a JavaScript Object is empty or not
If you don’t want to know the explanations, here’s the code snippet to help you check whether a JavaScript Object
is empty or not:
let emptyObj = {};
if (Object.keys(emptyObj).length === 0 && emptyObj.constructor === Object) {
console.log("Object is empty");
}
Read on to learn how and why it works.
To check whether a JavaScript Object
is empty or not, you can check whether the Object
variable has enumerable key names or not by using the Object.keys()
method.
#javascript #programming