Hello Friends 👋,

Welcome To Infinitbility! ❤️

This tutorial will help you to verify your object empty or not in javascript, here we will see examples of check empty object in Ecma 5, Pre-ECMA 5, jQuery, lodash, Underscore, Hoek, ExtJS, AngularJS (version 1), and Ramda.

Let’s take example of empty object.

const emptyObject = {};

Before going to check empty object, if want verify data is a object or not then you option to use javascript typeof keyword.

if(typeof emptyObject === "object"){
    // code goes here
}

JavaScript

JavaScript provide Object.entries() function to return an array containing the object’s enumerable properties.

Due to javascript codebase it will work also on React, React Native, typescript, node, and deno.

use like below:

Object.entries(emptyObject)

it returns an empty array, it means the object does not have any enumerable property, which in turn means it is empty.

  • Object.entries() if example
if(Object.entries(emptyObject).length === 0){
    // code goes here
}

ECMA 5+

Below empty object check example for ECMA 5+ developers.

if(Object.keys(obj).length === 0 && obj.constructor === Object){
    // code goes here
}

Pre-ECMA 5

Well if you are use ECMA previous versions.

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

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

jQuery

jQuery provide isEmptyObject() function to verify object empty or not, use like below example.

jQuery.isEmptyObject(emptyObject); // true
jQuery.isEmptyObject({ foo: "bar" }); // false

lodash

lodash provide isEmpty() function to verify object empty or not, use like below example.

_.isEmpty(emptyObject); // true

Underscore

Same like lodash, Underscore provide isEmpty() function to verify object empty or not, use like below example.

_.isEmpty({}); // true

Hoek

Hoek provide deepEqual() function to verify object data but you will also use for check empty object.

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

ExtJS

ExtJS provide Object prototype isEmpty() function to verify object empty or not, use like below example.

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

AngularJS

Same like Hoek, AngularJS provide equals() function to verify object data but you will also use for check empty object.

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

Ramda

Ramda provide isEmpty() function to verify object empty or not, use like below example.

R.isEmpty({}); // true

Thanks for reading…