How To Test For An Empty Object In JavaScript

Do you want to know how you can test for an Empty Object in JavaScript? There may be scenarios when you want to get the data from your Ajax request but the server does not have any update and return an empty object.

If you will not handle this Empty Object in JavaScript then you can get the error or there can be a runtime exception in your application.

To make sure you do not get the Error or Exceptions for Empty Objects in JavaScript you can either test your application properly and catch those errors or exceptions or edit your backend code so that it does not return an empty object instead returns a default object indicating no updates are available.

1. Javascript Check If Object Is Empty – Using keys Method

If you are using ECMAScript 5 or your application is having the support of this library then you can use the below code to test the empty object.

//Initializing An Empty Object
var emptyObj;

//Function to Test Empty Object That 
//Supports ECMAScript
function isObjectEmpty(inputObj){
    return Object.keys(inputObj).length === 0;
}

//Testing if Above Object is Empty
var isEmpty = isObjectEmpty(emptyObj);

//Printing the Result
console.log(isEmpty)

Output

true

As you can see in the above code since the emptyObj is empty and it has nothing the function to test this object returns True. As the keys Method takes the object as input and since there are currently no keys present and the length of the object will be zero in this case.

And using this function you can test any empty object and then you can handle that empty object as you want to handle it as per your application requirement.

2. Using For Loop To Test Empty Object in JavaScript

Alternatively, if ECMAScript is not supported in your application then you can use the For loop method to test if the given object is empty or not. Here I will be using the hasOwnProperty method that takes props as input and check if all the props are empty.

Let us see in the below example code how you can use the for loop to test the empty object in JavaScript.

//Initializing An Empty Object
var emptyObj;

//Function to Test Empty Object That 
//Does not Supports ECMAScript
function isObjectEmpty(inputObj){
    for(var prop in inputObj){
        if(Object.prototype.hasOwnProperty.call(inputObj, prop)){
            return false;
        }
    }
    return true;
}

//Testing if Above Object is Empty
var isEmpty = isObjectEmpty(emptyObj);

//Printing the Result
console.log(isEmpty)

Output

true

As you can see in the above code, the Empty object has no properties, and hence for loop checks if all the props present in the empty object are empty and if it is not then it returns false and if the props are empty then it will return true.

How To Test For An Empty Object In JavaScript

If you are the one who considers performance above anything then you need to use the below code to test the empty object. This one is the fastest among all of the above codes that are discussed here.

//Initializing An Empty Object
var emptyObj;

//Function to Test Empty Object That 
//Supports ECMAScript
function isObjectEmpty(inputObj){
    for(var prop in inputObj){ 
        return false;
    }
    return true;
}

//Testing if Above Object is Empty
var isEmpty = isObjectEmpty(emptyObj);

//Printing the Result
console.log(isEmpty)

Wrap Up

I hope you got the answer related to how to test empty objects in JavaScript and also in Javascript Check if the object is empty. I have listed the two methods above that you can use to test the empty object.

From the above two lists, the second method is the fastest method that you should use inside your code. As for loop is supported by all of the Library and the other props method can be used as well that can help you get the answer quickly.

If you have any questions or you know any better method than the one discussed above then please let me know in the comment section I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Code Examples: How To Format A Date in JavaScript
  2. How To Remove First Character From A String in JavaScript
  3. How To Fix Cannot use import statement outside a module JavaScript
  4. How To Remove A Specific item From An Array
  5. Best Udemy JavaScript Course 2021

Leave a Comment