JavaScript Fundamentals: Learning the Language of the Web

How to Quickly Check If an Array is empty or not in Javascript?

How to Quickly Check If an Array is empty or not in Javascript?

Introduction

In this article, we will see how to check an array if it's empty or not with array methods and detailed examples.

Approach: Using Array.isArray() method and array.length property

The Array.isArray() method can be used to determine whether an array is actually there and whether it exists. If the Object supplied as a parameter is an array, this function returns true. Additionally, it determines whether the array is undefined or null.

The array.length property can be used to determine whether the array is empty. The array's element count is returned by this attribute. The integer evaluates to true if it is bigger than 0.

When used with the AND(&&) operator, this method and property can be used to check whether the array is present and not empty.

Syntax

Array.isArray(emptyArray) && emptyArray.length

Example

//To check if an array is empty using javascript
function arrayIsEmpty(array) {
    //If  not an array, return FALSE.
    if (!Array.isArray(array)) {
        return FALSE;
    }
    //If it is an array, check its length property
    if (array.length == 0) {
        //Return TRUE if the array is empty
        return true;
    }
    //Otherwise, return FALSE.
    return false;
}

var charArr = new Array('a','b','c');

//An example of a JavaScript array that is empty.
var arrTwo = new Array();

print(arrayIsEmpty(charArr)); //returns FALSE
print(arrayIsEmpty(arrTwo)); //returns TRUE

write your code here: Coding Playground

Output

FALSE
TRUE

Here, we can see that charArr is an array and so meets the second requirement to determine whether the length of the array is empty. The function returns False since the array is not empty because it contains three elements. Since it is an array once more in the second scenario, arrTwo, it meets the second requirement. The function in this case returns True because the array is empty.

Example 2

print(Array.isArray([]));
print(Array.isArray([9]));
print(Array.isArray(new Array()));
print(Array.isArray(new Array('abc','def')));
print(Array.isArray(new Array(23)));
print(Array.isArray(Array.prototype));

write your code here: Coding Playground

Output

true
true
true
true
true
true

Note

The function Array.isArray() returns TRUE since Array.prototype is an array in and of itself.

Example 3

print(Array.isArray());
print(Array.isArray({}));
print(Array.isArray(null));
print(Array.isArray(undefined));
print(Array.isArray(21));
print(Array.isArray('Random String'));
print(Array.isArray(true));
print(Array.isArray(false));
print(Array.isArray(new Uint8Array(32)));
print(Array.isArray({
    __proto__: Array.prototype
}));

write your code here: Coding Playground

Output

false
false
false
false
false
false
false
false
false
false

Example 4


<head>
    <title>
        Check if an array is empty or not
    </title>
</head>

<body>
    <h1 style="color: rgb(27, 30, 200)">
        Javascript
    </h1>
 
    <b>
        Check if an array is empty or not
    </b>
 
    <p>
        Click on the button to check if
        array exists and is not empty
    </p>
 
    <p>emptyArray = []</p>
    <p>nonExistantArray = undefined</p>
    <p>fineArray = [23,56,89,12,34]</p>
 
    <p>
        Output for emptyArray:
        <span class="output-empty"></span>
    </p>
 
    <p>
        Output for nonExistantArray:
        <span class="output-non"></span>
    </p>
 
    <p>

write your code here: Coding Playground

Output

Conclusion

In this article, we understood the methods to check if an array is empty or not.