By falmp

2009-03-25 01:39:45 8 Comments

After an AJAX request, sometimes my application may return an empty object, like:

var a = {};

How can I check whether that's the case?


@Ron Jonk 2020-09-02 07:48:39

Mostly what you want to know is if the object has properties before using it. So instead asking isEmpty and then always check the negation like if(!isEmpy(obj)) you can instead just test if the object is not null and has properties instead

export function hasProperties(obj): boolean {
  return obj && obj.constructor === Object && Object.keys(obj).length >= 1;

@Dohd 2020-08-21 17:43:35

A simpler solution: var a = {};
Case a is empty: !Object.keys(a).length returns true.

@010011100101 2020-08-15 07:29:28

I liked this one I came up with, with the help of some other answers here. Thought I'd share it.

Object.defineProperty(Object.prototype, 'isEmpty', {
    get() {
        for(var p in this) {
            if (this.hasOwnProperty(p)) {return false}
        return true;

let users = {};
let colors = {primary: 'red'};
let sizes = {sm: 100, md: 200, lg: 300};

'\nusers =', users,
'\nusers.isEmpty ==> ' + users.isEmpty,
'\ncolors =', colors,
'\ncolors.isEmpty ==> ' + colors.isEmpty,
'\nsizes =', sizes,
'\nsizes.isEmpty ==> ' + sizes.isEmpty,

@Alex Tudor 2020-04-21 19:44:40

Best one-liner solution I could find (updated):

isEmpty = obj => !Object.values(obj).filter(e => typeof e !== 'undefined').length;
console.log(isEmpty({}))                                        // true
console.log(isEmpty({a: undefined, b: undefined}))              // true
console.log(isEmpty({a: undefined, b: void 1024, c: void 0}))   // true
console.log(isEmpty({a: [undefined, undefined]}))               // false
console.log(isEmpty({a: 1}))                                    // false
console.log(isEmpty({a: ''}))                                   // false
console.log(isEmpty({a: null, b: undefined}))                   // false

@Damien 2020-04-23 01:33:51

Greatest answer overlooked

@Ivanka Todorova 2020-05-07 12:33:28

@Damien, tbf the question is 11 years old & this answer was posted 2 weeks ago.

@Kunal Tyagi 2020-07-21 05:59:59

What if the object is like this: { 0 : null }, I am getting a key whose value is null. What to do in such cases?

@mickmackusa 2020-08-09 06:58:12

Checking Object.keys().length was already suggested on this question in 2009. So this posted answer is half flawed and the other half redundant.

@ganesh phirke 2020-07-19 17:08:46

We can check with vanilla js with handling null or undefined check also as follows,

function isEmptyObject(obj) {
  return !!obj && Object.keys(obj).length === 0 && obj.constructor === Object;


isEmptyObject(new Boolean());  // false 
isEmptyObject(new Array());    // false 
isEmptyObject(new RegExp());   // false 
isEmptyObject(new String());   // false 
isEmptyObject(new Number());   // false 
isEmptyObject(new Function()); // false 
isEmptyObject(new Date());     // false
isEmptyObject(null);          // false
isEmptyObject(undefined);     // false
isEmptyObject({});            // true

@Code 2020-07-19 05:53:29

1. Using Object.keys

Object.keys will return an Array, which contains the property names of the object. If the length of the array is 0, then we know that the object is empty.

function isEmpty(obj) {
    return Object.keys(obj).length === 0 && empty.constructor === Object;

We can also check this using Object.values and Object.entries. This is typically the easiest way to determine if an object is empty.

2. Looping over object properties with for…in

The for…in statement will loop through the enumerable property of object.

function isEmpty(obj) {
    for(var prop in obj) {
            return false;

    return true;

In the above code, we will loop through object properties and if an object has at least one property, then it will enter the loop and return false. If the object doesn’t have any properties then it will return true.

#3. Using JSON.stringify If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty.

function isEmptyObject(obj){
    return JSON.stringify(obj) === '{}';

4. Using jQuery


5. Using Underscore and Lodash



@Erik Töyrä Silfverswärd 2010-05-19 14:07:51

For those of you who have the same problem but use jQuery, you can use jQuery.isEmptyObject.

@MFD3000 2011-08-17 19:03:18

HEY! I just spent a few hours debugging IE 8 issues only to find that it was jQuery.isEmptyObject that was causing the problem. It returns true if the object is empty.

@Erik Töyrä Silfverswärd 2011-09-16 20:13:12

@MFD3000: Could you explain in detail what you did? Did you feed jQuery.isEmptyObject() with a string? In that case have a look at and

@Michał Miszczyszyn 2012-06-05 09:13:03

This won't work if you (or any plugin) modified Object.prototype.

@Eru 2012-10-01 14:10:49

Why do you post answer including jQuery if the question is not about jQuery at all?

@Александр Фишер 2012-12-12 19:44:55

I know its an old comment, but I wonder your question @MFD3000, because the docu says: returns true, if object is empty (as the name indicates it)

@Pablo Mescher 2013-02-27 17:04:56

including jQuery for such a basic task is not what I would call the right answer. It's true that nowadays jQuery is almost ubiquous, but still we shouldn't forget it is built around a very capable language itself.

@Paul Sanwald 2013-05-17 16:20:56

note that this works fine for the question asked, {}, but that jQuery.isEmptyObject([]) === true, presumably because an empty list is iterable.

@user1382306 2013-08-13 18:27:35

@eru he's psychic, knew that his answer would get the most votes. how'd he do that?

@DawnPaladin 2014-10-01 15:40:30

Note that jQuery.isEmptyObject returns false when you feed it an empty jQuery object (as noted on its API page). It only works for regular JavaScript objects.

@BadHorsie 2015-07-07 15:35:10

Typical JS snobbery in these comments. Everyone knows a huge proportion of JavaScript on the web is written on jQuery, so it is perfectly acceptable to provide a solution here for jQuery if it already has a built-in method for testing objects. It's likely that thousands of developers looking for help will find this answer helpful. Nobody said it's the only way to do it. I notice how nobody acts all elitist about the guy who posted a solution to use underscore.js...

@Tristan 2015-07-22 11:52:03

@BadHorsie that is bm. So each time a question about dates in javascript is asked we should answer with a random function of a date library? And the reason nobody acts 'elitist' about the underscore.js answer is only because he is not the accepted answer.

@BadHorsie 2015-07-22 12:19:24

@Tristan jQuery is hardly some random library, is it? It's only the most popular JS library of all time, on which millions of sites rely. I don't see the problem with posting both answers so that there is a pure JS solution, and a jQuery solution so developers who are working on jQuery sites know about a quick built-in function. Obviously nobody is suggesting you load the entire jQuery library on your site for the sole purpose of using one small utility function... It's pretty obviously meant to be a solution for people already using jQuery...

@Aakash Handa 2020-04-07 05:13:44

The new Way to check value is if(Object.entries(this.props.myarticle).length===0){ }

here myarticles is object

@Adam Zerner 2015-08-20 01:32:52

ECMA 5+:

// because Object.keys(new Date()).length === 0;
// we have to do some additional check
Object.keys(obj).length === 0 && obj.constructor === Object

Note, though, that this creates an unnecessary array (the return value of keys).

Pre-ECMA 5:

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

  return JSON.stringify(obj) === JSON.stringify({});


jQuery.isEmptyObject({}); // true


_.isEmpty({}); // true


_.isEmpty({}); // true


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


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

AngularJS (version 1)

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


R.isEmpty({}); // true

@James Dunne 2015-11-06 13:47:41

The Object.keys(...).length method does indeed check that the object is empty, but the return value of 0 is not exclusive to empty objects {}; it also returns 0 for [],"",0,1,function(){}, and I'm sure there are even more examples.

@Adesh M 2015-11-26 07:52:55

We'll prefer var data = {}; Object.keys(data).length; although data = {"a": []} returns 1 which sounds correct. Best while working with REST calls, where REST response can be handled on serverside otherwise go with data = {"success": "lorem ipsum"} or data = {"error": "lorem ipsum"}.

@davidhadas 2015-12-28 10:32:40

The Object.keys({}).length is 10 times slower than the ( option - I suggest to avoid it as a way to test if an objetc is empty.

@Mikhail 2016-02-09 11:21:38

Here is performance test between jQuery and underscore

@cjbarth 2016-03-01 16:34:38

Object.keys(new Date()).length === 0; so this answer can be misleading.

@Vivien Adnot 2016-04-12 08:10:28

@Adam Zerner can you explain why do you write return true && JSON.stringify(obj) === JSON.stringify({}); instead of just JSON.stringify(obj) === JSON.stringify({}); ?

@Adam Zerner 2016-04-12 15:45:46

@VivienAdnot To make sure that the input is actually an object, as opposed to a string or something. Without that, if the input is a string, it wouldn't enter the for in loop and would return true, which is wrong because a string isn't an empty object.

@Vivien Adnot 2016-04-13 07:56:32

@Adam Zerner Ok I understand thanks. So with this method, if you send a string in parameter you will receive false as result. Do you think the best solution is this or throwing an exception and receiving it in a try / catch block ?

@Adam Zerner 2016-04-13 14:40:52

Good point. Try-catch does seem more appropriate to me, but I'm not sure, and I was just aggregating others' answers here and the other answer didn't take that approach.

@John Nelson 2016-04-15 12:22:03

Instead of stringifying things you can also take advantage of this fact: (new Date()).constructor === Date, or conversely, ({}).constructor === Object.

@TheZver 2016-05-03 11:56:16

ExtJS: Ext.Object.isEmpty({})

@Jeremy A. West 2016-10-11 14:28:46

angular.equals({}, {});

@Agamemnus 2017-01-02 20:00:01

Is there any disadvantage in using == 0, as .length always returns a number?

@Erik Pukinskis 2017-07-31 19:15:04

What's the purpose of the JSON stringify at the end? Why not just return true?

@Jesse 2018-04-09 09:22:20

Object.keys() doesn't check non-enumerable properties, or symbols. You need to use Object.getOwnPropertyNames() and Object.getOwnPropertySymbols() instead. Also, Object.getPrototypeOf() is the correct way to get an object's prototype. obj.constructor can be easily forged. Example: function Foo() {} Foo.prototype.constructor = Object; (new Foo()).constructor === Object // true.

@Shasak 2018-06-06 15:43:41

the second solution it will also work with empty Arrays.

@Adriano Resende 2018-10-01 19:31:06

Code more simple possible: !!Object.keys(obj).length

@Alireza Mirian 2018-10-23 07:03:48

jQuery.isEmptyObject(new Date()); // true be wary of it.

@doup 2019-01-07 11:59:45

Beware with angular: angular.equals({ $foo: 'bar' }, {}) === true…

@Dalibor 2019-02-01 10:06:41

I tested for cases for loops of million iterations repeated test 10 times, and here are results of consecutive solutions: 1) ECMA 7+: 101 - 146 ms; 2) ECMA 5+ 20 - 37 ms; 3) classic solution: 638 - 669 ms; 4) jQuery: 20 - 32 ms; so the absolute winners are ECMA5 (preferably) and jQuery (optional, don't embed jQuery in your Angular project just because of that)

@Drenai 2019-02-07 17:12:05

@Dalibor You're not comparing like with like there. The ECMA 7/5+ examples are testing against new Date. jQuery skips this test, and fails with jQuery.isEmptyObject(new Date())

@Dalibor 2019-02-13 08:43:05

OK, so we can conclude that ECMA 5 is the winner :)

@DBrown 2019-03-02 04:08:58

The length checks can omit the strict equal and length value, since 0 will act as false, and any positive number will act as true. This was mentioned above with a double not operator, but this is a redundant practice when working with integers. i.e. [].length // 0 => false in condition, [1].length // => 1 true in condition, ![].length // 0 => true with not

@João Pimentel Ferreira 2019-04-30 20:20:29

the Pre-ECMA 5 function does not work with isEmpty([]). You may want to amend accordingly:

@Rohit Nair 2020-07-26 04:16:17

In pre-ECMA 5, instead of JSON.stringify(obj) === JSON.stringify({}) it could simply be JSON.stringify(obj) === '{}'

@AndyDaSilva52 2020-08-09 02:08:04

@Nirus 2014-10-20 11:28:24

This one line code helps with fallback to older browsers too.

var a = {}; //if empty returns false
(Object.getOwnPropertyNames ? Object.getOwnPropertyNames(a).length !== 0 : (function(){ for(var key in a) break; return !!key })()) //Returns False

var a = {b:2}; //if not empty returns true
(Object.getOwnPropertyNames ? Object.getOwnPropertyNames(a).length !== 0 : (function(){ for(var key in a) break; return !!key })()) //Returns true

Object.getOwnPropertyNames is implemented in ECMA-5. the above line works in older browsers with a fallback function.

Another quick solution is checking the length property of Object.keys, Object.entries or Object.values

Knowledge article: Follow this SO post for detailed difference between Object.keys Vs Object.getOwnPropertyNames

@Juan Vieira 2020-02-20 23:29:44

I know this doesn't answer 100% your question, but I have faced similar issues before and here's how I use to solve them:

I have an API that may return an empty object. Because I know what fields to expect from the API, I only check if any of the required fields are present or not.

For example:

API returns {} or {agentID: '1234' (required), address: '1234 lane' (opt),...}. In my calling function, I'll only check

if( && { 
  do something with my agentID 
} else { 
  is empty response

This way I don't need to use those expensive methods to check if an object is empty. The object will be empty for my calling function if it doesn't have the agentID field.

@will Farrell 2012-03-15 03:52:34

Here is a fast, simple, function:

function isEmptyFunction () {
  for (const i in this) return false
  return true

Implemented as a getter:

Object.defineProperty(Object.prototype, 'isEmpty', { get: isEmptyFunction })

console.log({}.isEmpty) // true

Implemented as a separate function:

const isEmpty =

console.log(isEmpty({})) // true

@Rob W 2012-05-07 14:01:02

Without Object.hasOwnProperty, this function will always return true (the function is called isEmpty, but returns true when it's not empty, by the way...). Also, the function will not invoke itself automatically. Add () after obj.isEmpty.

@Jeff Lambert 2012-07-05 13:59:56

Also, after just stumbling across this you would probably want to swap the return true/false statements. The function 'isEmpty' should return false once it finds a property, because that means that it is not empty. Semantics, but would probably be a good idea.

@Dmitry Sheiko 2018-06-27 11:24:01

It's weird that I haven't encountered a solution that compares the object's values as opposed to the existence of any entry (maybe I missed it among the many given solutions).
I would like to cover the case where an object is considered empty if all its values are undefined:

const isObjectEmpty = obj => Object.values(obj).every(val => typeof val === "undefined")

console.log(isObjectEmpty({}))                                 // true
console.log(isObjectEmpty({ foo: undefined, bar: undefined })) // true
console.log(isObjectEmpty({ foo: false,     bar: null }))      // false

Example usage

Let's say, for the sake of example, you have a function (paintOnCanvas) that destructs values from its argument (x, y and size). If all of them are undefined, they are to be left out of the resulting set of options. If not they are not, all of them are included.

function paintOnCanvas ({ brush, x, y, size }) {
  const baseOptions = { brush }
  const areaOptions = { x, y, size }
  const options = isObjectEmpty(areaOptions) ? baseOptions : { ...baseOptions, areaOptions }
  // ...

@kiranvj 2012-06-08 08:04:07

I am using this.

function isObjectEmpty(object) {
  var isEmpty = true;
  for (keys in object) {
     isEmpty = false;
     break; // exiting since we found that the object is not empty
  return isEmpty;


var myObject = {}; // Object is empty
var isEmpty  = isObjectEmpty(myObject); // will return true;

// populating the object
myObject = {"name":"John Smith","Address":"Kochi, Kerala"}; 

// check if the object is empty
isEmpty  = isObjectEmpty(myObject); // will return false;

from here



you can use the jQuery implementation of isEmptyObject

function isEmptyObject(obj) {
  var name;
  for (name in obj) {
    return false;
  return true;

@iman 2013-09-03 04:40:19

hi. when you test this function with number or boolean true or false return true and this is not correct result. isObjectEmpty(true). isObjectEmpty(false). isObjectEmpty(1)

@kiranvj 2013-09-03 06:31:47

We are checking whether the object is empty, not if the data type is an object. In your case to check if its an object we need to something like if(typeof a === "object") {...}

@Kamil Kiełczewski 2020-01-17 12:53:28


Today 2020.01.17 I perform tests on MacOs HighSierra 10.13.6 on Chrome v79.0, Safari v13.0.4 and Firefox v72.0, for chosen solutions.


  • solutions based on for-in (A,J,L,M) are fastest
  • solutions based on JSON.stringify (B,K) are slow
  • surprisingly also solution based on Object (N) is slow

enter image description here


Below in snippet are presented 15 solutions. If you want to run performance test on your machine click HERE.

var log = (s,f) => console.log(`${s} --> {}:${f({})}  {k:2}:${f({k:2})}`);

function A(obj) {
  for(var i in obj) return false; 
  return true;

function B(obj) {
  return JSON.stringify(obj) === '{}';

function C(obj) {
  return Object.keys(obj).length === 0;

function D(obj) {
  return Object.entries(obj).length === 0;

function E(obj) {
  return Object.getOwnPropertyNames(obj).length === 0;

function F(obj) {
  return Object.keys(obj).length === 0 && obj.constructor === Object;

function G(obj) {
  return typeof obj === "undefined" || !Boolean(Object.keys(obj)[0]);

function H(obj) {
  return Object.entries(obj).length === 0 && obj.constructor === Object;

function I(obj) {
  return Object.values( obj  ).every( val => typeof val === "undefined" );

function J(obj) {
  for (const key in obj) {
    if (, key)) {
      return false
  return true;

function K(obj) {
  for(var prop in obj) {
    if(obj.hasOwnProperty(prop)) {
      return false;

  return JSON.stringify(obj) == JSON.stringify({});

function L(obj) {
  for(var prop in obj) {
      return false;

  return true;

function M(obj) {
  for (var k in obj)
    if ( obj.hasOwnProperty(k) )
      return false;
  return true; 

function N(obj) {
  return Object.getOwnPropertyNames(obj).length === 0 &&
  Object.getOwnPropertySymbols(obj).length === 0 &&
  Object.getPrototypeOf(obj) === Object.prototype; 

function O(obj) {
  return !(Object.getOwnPropertyNames != undefined ? Object.getOwnPropertyNames(obj).length != 0 : (function(){for(var key in obj) break; return (key != null) && (key != undefined);})())


enter image description here

@Christian Matthew 2020-04-10 07:32:03

a lot of this doesn't make sense because you're basing everything on a return of false and or true. Sometimes programming needs an if statement or a ternary operator. just fyi

@Madacol 2020-06-13 00:08:58

For completeness, I edited your jsperf to test obj = {a:1,b:2,c:3} and for(var i in obj) is still the fastest

@Tarandeep Singh 2019-11-15 18:06:32

Perfect and failsafe solution

I think the first accepted solution works in most cases but is not Failsafe.

The better and failsafe solution will be.

function isEmptyObject() { 
  return === "[object Object]" 
  && Object.keys(obj).length === 0;

or in ES6/7

const isEmptyObject = () => === "[object Object]" 
  && Object.keys(obj).length === 0;

With this approach if the obj is set to undefined or null, the code does not break. and return null.

@Eksapsy 2019-11-27 15:38:11

You are checking if an object is empty with fixed strings and hacks/irregular practices like " This is just bad practice and code you dont wanna see in a codebase. There are much better and clean solutions that make sense just by reading them. This does not make sense while reading it, it's a hack.

@Ashikur Rahman 2019-11-15 14:12:26

    let jsObject = JSON.parse(JSON.stringify(obj), (key, value) => {
                if (value === null ||
                    value === '' ||
                    (value.constructor === Object && Object.entries(value).length === 0) ||
                    (value.constructor === Array && value.length === 0)) {
                    return undefined
                return value

This will filter out all the invalid fields recursively.

@davidhadas 2015-12-28 10:40:59

Using Object.keys(obj).length (as suggested above for ECMA 5+) is 10 times slower for empty objects! keep with the old school ( option.

Tested under Node, Chrome, Firefox and IE 9, it becomes evident that for most use cases:

  • ( is the fastest option to use!
  • Object.keys(obj).length is 10 times slower for empty objects
  • JSON.stringify(obj).length is always the slowest (not suprising)
  • Object.getOwnPropertyNames(obj).length takes longer than Object.keys(obj).length can be much longer on some systems.

Bottom line performance wise, use:

function isEmpty(obj) { 
   for (var x in obj) { return false; }
   return true;


function isEmpty(obj) {
   for (var x in obj) { if (obj.hasOwnProperty(x))  return false; }
   return true;

See detailed testing results and test code at Is object empty?

@yankee 2019-11-25 10:43:35

Object.keys is slow, but less code. On a small page, where this is called... maybe 10 times... Will this still be slower considering the additional parsing time of the additional code?

@Роман Татаринов 2019-10-23 15:29:04

isEmpty for value any type

/* eslint-disable no-nested-ternary */

const isEmpty = value => {
  switch (typeof value) {
    case 'undefined':
      return true;
    case 'object':
      return value === null
        ? true
        : Array.isArray(value)
        ? !value.length
        : Object.entries(value).length === 0 && value.constructor === Object;
    case 'string':
      return !value.length;
      return false;

@Alireza 2019-10-18 09:43:38

That's similar way of how it gets checked in lodash source for object :

const isEmpty = value => {
  for (const key in value) {
    if (, key)) {
      return false
  return true;

But there are many other ways to do that.

@Anthony D'Amato 2019-05-23 08:07:56

To really accept ONLY {}, the best way to do it in Javascript using Lodash is:

_.isEmpty(value) && _.isPlainObject(value)

@João Pimentel Ferreira 2019-04-19 17:31:46

Pure Vanilla Javascript, and full backward compatibility

function isObjectDefined (Obj) {
  if (Obj === null || typeof Obj !== 'object' || === '[object Array]') {
    return false
  } else {
    for (var prop in Obj) {
      if (Obj.hasOwnProperty(prop)) {
        return true
    return JSON.stringify(Obj) !== JSON.stringify({})

console.log(isObjectDefined()) // false
console.log(isObjectDefined('')) // false
console.log(isObjectDefined(1)) // false
console.log(isObjectDefined('string')) // false
console.log(isObjectDefined(NaN)) // false
console.log(isObjectDefined(null)) // false
console.log(isObjectDefined({})) // false
console.log(isObjectDefined([])) // false
console.log(isObjectDefined({a: ''})) // true

@Jplus2 2019-03-21 01:56:41

export function isObjectEmpty(obj) {
  return (
    Object.keys(obj).length === 0 &&
    Object.getOwnPropertySymbols(obj).length === 0 &&
    obj.constructor === Object

This include checking for objects containing symbol properties.

Object.keys does not retrieve symbol properties.

@ahmadalibaloch 2014-05-15 12:51:14

Under the hood all empty check methods in all libraries use object keys checking logic. Its an odd way to make it understandable, which you can put in a method, Described here.

for(key in obj){
   //your work here.

Which has evolved in ES5, now put simply you can check the object's keys length, using Object.Keys method which takes your object as it's parameter:

if(Object.keys(obj).length > 0){
 //do your work here

Or if you are using Lodash (you must be) then.

 _.isEmpty(obj) //==true or false

@Soren 2014-06-29 01:31:41

While is is correct as an odd way of making an if-statement -- it will probably confuse somebody who will maintain the code after you.

@Tudor Morar 2018-09-11 10:35:16

I would go for checking if it has at least one key. That would suffice to tell me that it's not empty.

typeof obj !== "undefined" && Boolean(Object.keys(obj)[0])

@Jimmy Obonyo Abor 2018-09-25 01:36:01

what if the first key returns false value ? the result will be false which is incorrect .

@Tudor Morar 2018-09-25 10:03:42

I have tested for that. Can you give a working example?

@Mrinmoy 2018-12-04 23:16:36

this is short and concise, but will result in runtime error if the Object is undefined

@Adrian Bartholomew 2020-09-02 18:05:59

@ Jimmy Obonyo Abor How can a key be false?

@Vikrant 2018-12-29 09:11:28

As per the ES2017 specification on Object.entries(), the check is simple using any modern browser--

Object.entries({}).length === 0

@faintsignal 2019-01-11 21:05:49

Is there any benefit to using this over Object.keys or Object.values?

@Vikrant 2019-01-13 05:23:52

@faintsignal using those are perfectly fine. I just added entries as did not find it in the comments.

@Jonathan 2018-08-14 21:49:51

This is what I came up with, to tell if there are any non-null values in the object.

function isEmpty(obj: Object): Boolean {
    for (const prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            if (obj[prop] instanceof Object) {
                const rtn = this.isEmpty(obj[prop]);
                if (rtn === false) {
                  return false;
            } else if (obj[prop] || obj[prop] === false) {
                return false;
    return true;

@Imran Ahmad 2018-08-09 19:13:31

Meanwhile we can have one function that checks for all 'empties' like null, undefined, '', ' ', {}, [].

var isEmpty = function(data) {
    if(typeof(data) === 'object'){
        if(JSON.stringify(data) === '{}' || JSON.stringify(data) === '[]'){
            return true;
        }else if(!data){
            return true;
        return false;
    }else if(typeof(data) === 'string'){
            return true;
        return false;
    }else if(typeof(data) === 'undefined'){
        return true;
        return false;

Use cases and results.

console.log(isEmpty()); // true
console.log(isEmpty(null)); // true
console.log(isEmpty('')); // true
console.log(isEmpty('  ')); // true
console.log(isEmpty(undefined)); // true
console.log(isEmpty({})); // true
console.log(isEmpty([])); // true
console.log(isEmpty(0)); // false
console.log(isEmpty('Hey')); // false

@wizebin 2018-11-15 04:36:20

You shouldn't stringify your data in a utility function like this. Imagine if you had an object with 1GB of data stored in it, and decided to run this function on it- you'd stringify the whole object just to check to see if it has anything inside it? Just use Object.keys()

@GibboK 2017-03-29 10:42:36

The following example show how to test if a JavaScript object is empty, if by empty we means has no own properties to it.

The script works on ES6.

const isEmpty = (obj) => {
    if (obj === null ||
        obj === undefined ||
        Array.isArray(obj) ||
        typeof obj !== 'object'
    ) {
        return true;
    return Object.getOwnPropertyNames(obj).length === 0;
console.log(isEmpty(''));           // true
console.log(isEmpty(33));           // true
console.log(isEmpty([]));           // true
console.log(isEmpty({}));           // true
console.log(isEmpty({ length: 0, custom_property: [] })); // false
console.log(isEmpty('Hello'));      // true
console.log(isEmpty([1, 2, 3]));    // true
console.log(isEmpty({ test: 1 }));  // false
console.log(isEmpty({ length: 3, custom_property: [1, 2, 3] })); // false
console.log(isEmpty(new Date()));   // true
console.log(isEmpty(Infinity));     // true
console.log(isEmpty(null));         // true
console.log(isEmpty(undefined));    // true

@Jesse 2018-04-09 09:37:41

The correct answer is:

const isEmptyObject = obj =>
  Object.getOwnPropertyNames(obj).length === 0 &&
  Object.getOwnPropertySymbols(obj).length === 0 &&
  Object.getPrototypeOf(obj) === Object.prototype;

This checks that:

  • The object has no own properties (regardless of enumerability).
  • The object has no own property symbols.
  • The object's prototype is exactly Object.prototype.

In other words, the object is indistinguishable from one created with {}.

Related Questions

Sponsored Content

63 Answered Questions

[SOLVED] How can I merge properties of two JavaScript objects dynamically?

46 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

86 Answered Questions

[SOLVED] How do JavaScript closures work?

61 Answered Questions

[SOLVED] How do I include a JavaScript file in another JavaScript file?

54 Answered Questions

39 Answered Questions

[SOLVED] Length of a JavaScript object

49 Answered Questions

[SOLVED] How to check if an object is an array?

70 Answered Questions

[SOLVED] How do I correctly clone a JavaScript object?

3 Answered Questions

67 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

Sponsored Content