Javascript: Check If an Object Empty

While working in javascript, we often encounter a requirement to determine if the object is empty. This check is required at times before operating on the properties or methods of the objects. This article discusses various ways to check if an object is empty using example illustrations.

Javascript objects are something that describes anything with properties and methods.

An object is empty when defined but does not contain any properties or methods in it. For example:

let myObject = {} 

Table of Contents:

Check If an Object is Empty using for in and hasOwnProperty()

Javascript’s hasOwnProperty() determines if the object has the specified property as its own property(not inherited). This method returns a boolean value.

Advertisements

Example:-

Check if the below objects are empty

  • personObject1 = { personFirstName : ‘George’, personLastName : ‘Smith’, dateOfBirth : ‘Nov 14 1984’ , city : ‘Santiago’ }
  • personObject2 ={}

Code:-

// function to check if the object is empty
function isObjectEmpty(_object) 
{
  let isEmpty = false;
  for(let p in _object) 
  {
    if(Object.prototype.hasOwnProperty.call(_object, p))
    {
      isEmpty = true;
    }
  }
  return !isEmpty;
}
let personObject1 =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };
let personObject2 = {} ;    
//usage of the function
console.log(isObjectEmpty(personObject1));  //false
console.log(isObjectEmpty(personObject2));  //true

Output:-

false
true

Explanation:-

Here, in the above code, we created a function(isObjectEmpty) to check if the object is empty or not.
The function takes an object as the parameter and returns true if the object is empty. Else returns false.
Within the function, we are looping through all the properties of the object. Even if one property is found, the variable isEmpty gets assigned a value ‘true’.
Initially, isEmpty is assigned as false. In the end, the variable isEmpty is returned.

Note that this function can be used even before ECMAScript5.

Check If an Object is Empty using Object.keys()

Javascript’s Object.keys() will return an array of property names of the object iterated in the order as in a standard loop.

Javascript’s Object.getPrototypeOf() will return the internal [[Prototype]]] property of the specified object.

Example:-

Check if the below objects are empty

  • personObject1 = { personFirstName : ‘George’, personLastName : ‘Smith’, dateOfBirth : ‘Nov 14 1984’ , city : ‘Santiago’ }
  • personObject2 ={}

Code:-

// function to check if the object is empty
function isObjectEmpty(_object)
{
  // null check
  if(_object !== null)
  {
  return Object.keys(_object).length === 0 && Object.getPrototypeOf(_object) === Object.prototype;
  }
  return true;
}
let personObject1 =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };
let personObject2 = {} ;    
//usage of the function
console.log(isObjectEmpty(personObject1));//false
console.log(isObjectEmpty(personObject2));//true

Output:-

false
true

Explanation:-

Here, in the above code, we created a function(isObjectEmpty) to check if the object is empty or not.
The function takes an object as the parameter and returns true if the object is empty. Else returns false.
Within the function, we are first checking if the object is null. If the object is not null we check for two statements. One is to find out that the length of the array returned by Object.keys ===0, and the second is to find out if the internal prototype property is equal to Object.prototype. Only if both the statements are true, object will be empty. If any statement is false, it means the object is not empty.

Note that this method can be used ECMAScript5 onwards.

Check If an Object is Empty using JSON.stringify()

The JSON.stringify() will convert a javascript object or value to a JSON string.

Example:-

Check if the below objects are empty

  • personObject1 = { personFirstName : ‘George’, personLastName : ‘Smith’, dateOfBirth : ‘Nov 14 1984’ , city : ‘Santiago’ }
  • personObject2 ={}

Code:-

// function to check if the object is empty
function isObjectEmpty(_object)
{
  if(_object !== null)
  {
  return JSON.stringify(_object) === '{}';
  }
  return true;
}
let personObject1 =  { personFirstName : 'George',
                      personLastName  : 'Smith',
                      dateOfBirth     : 'Nov 14 1984' ,
                      city            : 'Santiago'
                    };
let personObject2 = {} ;    
//usage of the function
console.log(isObjectEmpty(personObject1));//false
console.log(isObjectEmpty(personObject2));//true

Output:-

false
true

Explanation:-

Here, in the above code, we created a function(isObjectEmpty) to check if the object is empty or not.
The function takes an object as the parameter and returns true if the object is empty. Else returns false.
Within the function, we are first checking if the object is null. If the object is not null, we check for a statement JSON.stringify(_object) === ‘{}’ , which returns true if none of the property is found within the object. Else returns false.

Check If an Object is Empty in jQuery

JQuery is a javascript library.

jQuery.isEmptyObject({}); 

This method will return true if the object is empty.

Check If an Object is Empty in lodash

lodash is a javascript utility library.

_.isEmpty({}); 

This method will return true if the object is empty.

Check If an Object is Empty in Underscore

Underscore is a javascript library that provides functional programming without extending built-in objects.

_.isEmpty({});

This method will return true if the object is empty.

Check If an Object is Empty in Ramda

Ramda is a javascript library of functions to add functional programming to javascript.

R.isEmpty({});

This method will return true if the object is empty.

I hope this article helped you check if the object is empty in javascript. Good Luck !!!

Advertisements

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top