You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

Copyfunction isEmpty(obj) {
  for (const prop in obj) {
    if (Object.hasOwn(obj, prop)) {
      return false;
    }
  }

  return true;
}

If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:

Copyfunction isEmptyObject(value) {
  if (value == null) {
    // null or undefined
    return false;
  }

  if (typeof value !== 'object') {
    // boolean, number, string, function, etc.
    return false;
  }

  const proto = Object.getPrototypeOf(value);

  // consider `Object.create(null)`, commonly used as a safe map
  // before `Map` support, an empty object as well as `{}`
  if (proto !== null && proto !== Object.prototype) {
    return false;
  }

  return isEmpty(value);
}

Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.

Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).

For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:

Copyfunction isEmpty(obj) {
  for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return false;
    }
  }

  return true
}

Many popular libraries also provide functions to check for empty objects:

jQuery:

CopyjQuery.isEmptyObject({}); // true

lodash:

Copy_.isEmpty({}); // true

Underscore:

Copy_.isEmpty({}); // true

Hoek:

CopyHoek.deepEqual({}, {}); // true

ExtJS:

CopyExt.Object.isEmpty({}); // true

AngularJS (version 1):

Copyangular.equals({}, {}); // true

Ramda:

CopyR.isEmpty({}); // true
Top answer
1 of 16
7555

You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:

Copyfunction isEmpty(obj) {
  for (const prop in obj) {
    if (Object.hasOwn(obj, prop)) {
      return false;
    }
  }

  return true;
}

If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:

Copyfunction isEmptyObject(value) {
  if (value == null) {
    // null or undefined
    return false;
  }

  if (typeof value !== 'object') {
    // boolean, number, string, function, etc.
    return false;
  }

  const proto = Object.getPrototypeOf(value);

  // consider `Object.create(null)`, commonly used as a safe map
  // before `Map` support, an empty object as well as `{}`
  if (proto !== null && proto !== Object.prototype) {
    return false;
  }

  return isEmpty(value);
}

Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.

Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).

For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:

Copyfunction isEmpty(obj) {
  for (var prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      return false;
    }
  }

  return true
}

Many popular libraries also provide functions to check for empty objects:

jQuery:

CopyjQuery.isEmptyObject({}); // true

lodash:

Copy_.isEmpty({}); // true

Underscore:

Copy_.isEmpty({}); // true

Hoek:

CopyHoek.deepEqual({}, {}); // true

ExtJS:

CopyExt.Object.isEmpty({}); // true

AngularJS (version 1):

Copyangular.equals({}, {}); // true

Ramda:

CopyR.isEmpty({}); // true
2 of 16
1504

If ECMAScript 5 support is available, you can use Object.keys():

Copyfunction isEmpty(obj) {
    return Object.keys(obj).length === 0;
}

For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:

Copyfunction isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
🌐
freeCodeCamp
freecodecamp.org › news › check-if-an-object-is-empty-in-javascript
How to Check if an Object is Empty in JavaScript – JS Java isEmpty Equivalent
November 7, 2024 - You can also make use of the JSON.stingify() method, which is used to convert a JavaScript value to a JSON string. This means it will convert your object values to a sting of the object. For example: let userDetails = { name: "John Doe", username: "jonnydoe", age: 14 }; console.log(JSON.stringify(userDetails)); Output: "{'name':'John Doe','username':'jonnydoe','age':14}" This means when it is an empty object, then it will return "{}". You can make use of this to check for an empty object.
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to check if a json object is empty or not in javascript
How to Check if a JSON Object is Empty or Not in JavaScript
March 28, 2024 - This method includes a check to see if the object is null or undefined, along with verifying if the object has no enumerable properties. const isEmpty = (obj) => { return obj === null || obj === undefined || Object.keys(obj).length === 0; }; const myObject = {}; console.log(isEmpty(myObject)); // Output: true · Incorporate these methods into your JavaScript projects to efficiently validate and handle empty JSON objects.
🌐
Sentry
sentry.io › sentry answers › javascript › how do i test for an empty javascript object?
How do I Test for an Empty JavaScript Object? | Sentry
This method was used as an alternative ... is widely supported by browsers. You can use JSON.stringify() to convert the value to a JSON string to check if the value is an empty object....
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-check-object-is-empty-or-not › m-p › 2943062
How to check Object is Empty Or not - ServiceNow Community
June 20, 2024 - Hi All, Can you please suggest me how to check Object is empty or not, If JSON is empty then complier should not go till " return JSON.stringify(arr);" if JSON is not empty then complier should touch return JSON.stringify(arr); line in Code. issue: script include returning error message like "Em...
Find elsewhere
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › How-to-check-if-JSON-content-is-null-or-empty › td-p › 237344
How to check if JSON content is null (or empty)? - Cloudera Community - 237344
August 17, 2020 - Make sure to mark the answer as the accepted solution. If you find a reply useful, say thanks by clicking on the thumbs up button. ... To check null in JavaScript, use triple equals operator(===) or Object is() method.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
How to Check if an Object Is Empty in JavaScript | Envato Tuts+
April 29, 2022 - The JSON.stringify method is used to convert a JavaScript object to a JSON string. So we can use it to convert an object to a string, and we can compare the result with {} to check if the given object is empty.
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-an-object-is-empty-using-javascript
How to check if an object is empty using JavaScript?
In the above syntax, JSON.stringify() method returns the "{}" if the education object is empty. In the example below, we have created the education object, which contains some properties. So, JSON.stringify() method will not return ?{}', but it will return the string value of the education object.
🌐
Built In
builtin.com › software-engineering-perspectives › javascript-check-if-object-is-empty
How to Check If an Object Is Empty in JavaScript | Built In
function isEmptyObject(obj){ return JSON.stringify(obj) === '{}' } jQuery.isEmptyObject(obj); Related ReadingWhat Is the @ Symbol in Python and How Do I Use It? _.isEmpty(obj); These five quick and easy ways to check if an object is empty in JavaScript are all you need to get going.
🌐
Arunkumar Blog
arungudelli.com › home › tutorial › javascript › 8 ways to check if an object is empty or not in javascript
8 ways To Check If An Object Is Empty or not In JavaScript
July 6, 2020 - We can convert the javascript empty object to JSON string and check with JSON.stringify({}) function ifObjectIsEmpty(object){ var isEmpty=true; if(JSON.stringify(object)==JSON.stringify({})){ // Object is Empty isEmpty = true; } else{ //Object ...
🌐
Codedamn
codedamn.com › news › javascript
How to check if an object is empty in JavaScript?
November 18, 2022 - If one of the condition become true we will get our expected result. So hence can find if the object is empty or not using the JSON.stringify() method. In This article, we learn about what an object in JavaScript is.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-check-an-object-is-empty-using-javascript
How to Check an Object is Empty using JavaScript? - GeeksforGeeks
July 11, 2025 - This method is useful for older ... Empty Object") }; ... The JSON.stringify() method converts the object to a JSON string and then use "===" operator to check the object is empty or not....