In this article, you will learn how to check if an object has any properties in JavaScript.
To check if an object has a property or is empty, you can use the Object.keys() method which returns an array with all the properties of your object.
let obj = {
name:"John",
age:40
};
console.log(Object.keys(obj))
/* output
▶️Array
0:name
1:age
length:2
*/
So, using its length property, you can check whether the above object contains any property.
<script>
let obj = {
name:"John",
age:40
};
if(Object.keys(obj).length){
console.log('Object is not blank!')
} else{
console.log('Object is blank!')
}
</script>
Comments (0)