Skip to content

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 checks if the object is empty

Simple example code Access the length property on the array and check object is empty or 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

Using a loop:

function isEmptyObject(obj) {
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      return false;
  return true;

const myObject = {};
console.log(isEmptyObject(myObject));  // Output: true = 'bar';
console.log(isEmptyObject(myObject));  // Output: false

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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading