How to check if an object is empty in JavaScript?
Tags: javascript
Published: 2021-06-12 00:00:00

---
  title: How to check if an object is empty in JavaScript?
  published: true
  tags: JavaScript
  date: Sat Jun 12 2021 05:30:00 GMT+0530 (India Standard Time)
  canonical_url: https://melvingeorge.me/blog/check-if-object-is-empty-javascript
  cover_image: https://melvingeorge.me/_next/static/images/main-9b4853052c85e8236978b37f87c654d7.jpg
  ---
  [Originally posted here!](https://melvingeorge.me/blog/check-if-object-is-empty-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.

### TL;DR

```js
// 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,

```js
// 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,

```js
// 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,

```js
// 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,

```js
// 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](https://jsbin.com/xaviteqahe/2/edit?js,console)

We can also do the same using a `conditional (ternary) operator` like this,

```js
// 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](https://jsbin.com/kukazexopu/2/edit?js,console)

That's all 😃!

### Feel free to share if you found this useful 😃.

---