Javascript programming articles and tutorials for beginner | Anjan Dutta

Check if object is empty javascript

Check if object is empty javascript

The javascript object doesn't have a length property. So, to check if a javascript object is empty or not, we can check the number of keys to determine if an object is empty or not.

The code looks like below:

let sampleObj = {};
if (Object.keys(sampleObj).length === 0) {
.......
// Your logic goes here
.......
}

In the above example, the condition Object.keys(sampleObj).length === 0 is checking the empty logic.

Rest, you can place your own logic in the if condition body to implement your requirement.

Edit this page on GitHub