Simplest Way to Check for Empty Objects in JavaScript

Simplest Way to Check for Empty Objects in JavaScript

Check for empty objects in JavaScript with one line
Ferenc Almasi β€’ Last updated 2022 June 20 β€’ Read time 2 min read
  • twitter
  • facebook
JavaScript

The easiest way to check if an object is empty in JavaScript is to use a combination of Object.keys and a constructor check:

  Object.keys(emptyObject).length === 0 && emptyObject.constructor === Object
Copied to clipboard! Playground
// How to check if an object is empty using an if statement
const emptyObject = {};

if (Object.keys(emptyObject).length === 0 && emptyObject.constructor === Object) {
  // πŸ‘Œ This means your object is empty
}
empty.js

Object.keys returns an array of the property names based on the passed object. But why do we need the second check for the constructor?

This is because Object.keys alone will return true if you pass anything else than an object:

Copied to clipboard!
Object.keys(0).length    === 0; // This will return true
Object.keys([]).length   === 0; // This will return true
Object.keys(true).length === 0; // This will return true
Object.keys('').length   === 0; // This will return true
empty.js

Even though we are not checking against an object, but other arbitrary data, the equation will return true. This is why we must check if the constructor is of type Object.


Browser Support

While Object.keys has fairly good support, if you need to fall back to older browsers, you can use the following solution:

Copied to clipboard! Playground
// Fallback for older browsers
obj.constructor === Object && JSON.stringify(obj) === '{}';

// or
Object.prototype.toString.call(obj) === '[object Object]' && JSON.stringify(obj) === '{}';
empty.js

This solution makes use of JSON.stringify, which is used for converting objects to JSON strings.

Keep in mind that for both solutions, you will get an error if you try to use it on a null or undefined value.

Uncaught TypeError: Cannot convert undefined or null to object

To get around this, you first need to check if the variable exists:

Copied to clipboard! Playground
// Prevent errors for null and undefined

const emptyObject = {};

// The first check here is used for filtering out null and undefined
if (emptyObject && Object.keys(emptyObject).length === 0 && emptyObject.constructor === Object) {
  // πŸ‘Œ This means your object is empty
}
empty.js

Or wrapping the whole thing into a function:

Copied to clipboard! Playground
const isEmptyObject = object => {
  if (Object.keys) {
    return object &&
           Object.keys(object).length === 0 &&
           object.constructor === Object;
  }

  return obj.constructor === Object && JSON.stringify(obj) === '{}';
};

isEmptyObject({}); // Returns true
isEmptyObject([]); // Returns false
isEmptyObject(0);  // Returns false
isEmptyObject.js
How to Check if an Object is Empty in JavaScript
If you would like to see more Webtips, follow @flowforfrank

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Access 100+ interactive lessons
  • check Unlimited access to hundreds of tutorials
  • check Prepare for technical interviews
Become a Pro

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.