JavaScript check if object is empty | Example code

  • by

Use the object key method to get the length of an object then check if the object is empty in JavaScript. SimpleCheck if the length of keys is equal to 0, if it is, then the object is empty.

JavaScript check if object is empty

Simple example code Access the length property on the array and check object empty to not.

<!DOCTYPE html>

    const obj = {};

    const isEmpty = Object.keys(obj).length === 0;
    if (isEmpty) {
      console.log("Given object is empty",isEmpty)



JavaScript check if object is empty

An alternative method is to try to iterate over the properties of the object. If there is even a single iteration, then the object is not empty.

// Supported in IE 6-11
const obj = {};

function isEmpty(object) {
  for (const property in object) {
    return false;
  return true;

console.log(isEmpty(obj)); // 👉️ true

Do comment if you have any doubts or suggestions on this JS object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.