πŸŽ‰ We just launched a new HTML course! Get 3 courses with 33% off! πŸŽ‰
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 β€’ πŸ”„ 2022 June 20 β€’ πŸ“– 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
// 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
Copied to clipboard!

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:

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
Copied to clipboard!

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.

Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.
Master JavaScript

Browser Support

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

// Fallback for older browsers
obj.constructor === Object && JSON.stringify(obj) === '{}';

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

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:

// 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
Copied to clipboard!

Or wrapping the whole thing into a function:

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
Copied to clipboard!
How to Check if an Object is Empty in JavaScript
If you would like to see more Webtips, follow @flowforfrank

Resources:

Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundreds of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_~