Check if JavaScript array is empty, null or undefined in 4 ways

JavaScript array is empty

It is very easy to check if JavaScript array or object is empty but might need additional checks if you want to also check for null or undefined.

[su_note note_color=”#FFF9C4″]For checking the emptiness of an array we will use array.length property in most of our examples. It returns the number of elements present in the array. Basically size of an array.

If the number is greater than 0, it also evaluates to true.[/su_note]

However, handling of a javascript object requires a different way.

Here are multiple ways to check for empty object or array in javascript :

1. Check if an array is empty

This is a very basic method to check if the object is empty using the if-else condition.

Example :

var testArray = [];
if (testArray && testArray.length > 0) {
   console.log('testArray is not empty.');
}else{
   console.log('testArray is empty.');
}

Output :

testArray is empty.

[su_label type=”success”]Suggested read : [/su_label] Best JavaScript Frameworks trending

2. Check if var is an array then is empty?

What if we want to first check if a var is of type array and then check length of the javascript array? Here is a way to achieve it.

Example :

var testArray = [1,2,3];
if (Array.isArray(testArray) && emptyArray.length) {
    console.log('testArray is not empty.');
}else{
    console.log('testArray is empty.');
}

Output :

testArray is not empty.

In the above example, since both the method and property returns true, the flow goes into if part. Had array been empty or object type it would have gone to else part.

Failing example :

var items = {};
if (Array.isArray(items) && items.length) {
    console.log('items is not empty array.');
}else{
    console.log('items is not array.');
}

Output :

items is not array.

[su_label type=”success”]Suggested read : [/su_label] Create JavaScript key value array pairs using easy examples

3. Validate the undefined, null and length of an array

There can be cases where the array doesn’t exist at all or remains uninitialized.

We can have a check over such cases also and verify for the length of arrays.

We will check the array for undefined or nullwith the help of typeof an operator. If these check pass then we can check for the length of array ultimately.

If length of the array is greater than zero, voila our conditions pass.

Example :

var items = [1,2,3];
if (typeof   items != 'undefined' && items != null && items.length > 0) {
    console.log('items is not empty array.');
}else{
   console.log('items is empty array.');
}

Output :

testArray is not empty array.

[su_label type=”success”]Suggested read : [/su_label] JavaScript String Includes and How To Use Examples

4. Check if the object is empty

I will give you the shortest way to check if the javascript object is empty. Here is a readymade helper function to validate any object for nonemptiness.

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

What this function does is it will try to get the key from the object and check if that key exists in the original object. If there are no keys, the flow fails and returns false.

Example of how to use this function :

var items = {}; // Empty Object
if(isEmpty(items)) {
    console.log('items is empty array.');
} else {
     console.log('items is not empty array.');
}

Alternatively , I found this solution also on internet, haven’t tried but could be a good solution.

var isItemsEmpty = !Object.keys(items).length;

Keep this guide bookmarked or have this helper function handy. This will be used very frequently if you are a Javascript developer.

Leave a Comment

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

Scroll to Top