How to check if an object is empty in JavaScript

For JavaScript objects, there is no built-in .length or .isEmpty method available to check if they are empty.

Here are 4 different methods that you can use to make sure that a given object does not have its own properties:

Object.entries() Method

The Object.entries() method takes an object as an argument and returns an array of its enumerable property [key, value] pairs.

We can then use the .length property of the array to check if it contains any item:

const user = {}

Object.entries(user).length === 0 // true

const bird = {
  name: 'Penguin',
  avatar: '🐧'

Object.entries(bird).length === 0 // false

Note: Object.entries() only works in modern browsers and is not supported by IE. If you need backward compatibility, consider adding a polyfill or use Oject.keys() method instead.

Object.keys() Method

The Object.keys() method is the best way to check if an object is empty because it is supported by almost all browsers, including IE9+.

It returns an array of a given object's own property names. So we can simply check the length of the array afterward:

Object.keys({}).length === 0 // true

Object.keys({ name: 'Atta' }).length === 0 // false

Object.getOwnPropertyNames() Method

The Object.getOwnPropertyNames() method takes an object as parameter and returns an array of its own property names:

Object.getOwnPropertyNames({}).length === 0 // true

Object.getOwnPropertyNames({ id: 1, name: 'John Doe' }).length === 0 // false

3rd-Party Libraries

If you are already using 3rd-party libraries like jQuery or Lodash in your web application, you can also use them to check if an object is empty:

// jQuery
jQuery.isEmptyObject({}) // true

jQuery.isEmptyObject({ id: 1, name: 'John Doe' }) // false

// Lodash
_.isEmpty({}) // true

_.isEmpty({ name: 'Emma' }) // false

Prototype Function

You can write a helper function isEmpty() and add it to the object's prototype:

Object.prototype.isEmpty = function () {
  return Object.keys(this).length == 0

Now you can call the isEmpty() method on any JavaScript object to check if it has its own properties:

const obj = {}

obj.isEmpty() // true

const site = {
  title: 'Twitter',
  website: ''

site.isEmpty() // false

Be careful while extending the Object prototype, as it can cause issues when used together with other JavaScript frameworks. Instead, use the Object.keys() method in JavaScript applications to check if an object is empty.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.