Signup/Sign In

Check for Empty Array in Javascript

Posted in Programming   LAST UPDATED: JUNE 3, 2021

    While writing a program or any script or any piece of frontend code in Javascript, if you want to check if an array is empty or not, you can use the length property of arrays in Javascript.

    The length property of the array returns the count of the number of elements stored in the array. So if the length property returns 0 as output that means the array is empty.

    Let's see a few examples for this to understand this better.

    Using Array's length property:

    It is super easy to use the length property of the array in Javascript. We use the . (dot) operator and then the name of the property which is length.

    Here is the syntax:

    myArr.length;

    The above code will return the count of the elements stored in the array myArr.

    Check if Array is Empty:

    There are many ways to do this:

    1. By directly printing the value returned by length property

    2. Using an if condition

    3. Using the logical NOT operator

    Let's see code examples for all these different ways.

    1. Print Array Element Count:

    If we have an array referenced by a variable, like this,

    let myArr = ['Bugati', 'Honda', 'Ferrari'];

    Then we can use the following code to print the length of the above array in the console,

    console.log(myArr.length);


    3

    The output of the above code will be 3, which you can see in the developer console of your browser. When the output is 0, means the array is empty.

    2. Using an if condition

    This is a more complete way of doing this and I would recommend this approach because the code we write should be able to find itself through condition checking whether the array is empty or not.

    Agan, let's take a variable refrencing an array,

    // empty array
    let newArr = [];

    Now, if we have to write a script, to find out if a given array is empty or not, we can use the length property and the if condition to compare the length value with 0, using the == operator.

    if(newArr.length == 0) {
        console.log("Array is empty");
    }
    else {
        console.log("Array has data");
    }


    Array is empty

    As our array was empty this time, hence we get the above output, because the length property returned 0.

    3. Using the logical NOT operator

    When we use the NOT operator(!) along with length property expression, then it will return true if the array is empty, because 0 is also treated as false, and negation of false is true.

    Let's take a quick example to understand this. Again we have an empty array below,

    let arr = [];

    Now, we will use the NOT operator,

    console.log(!arr.length);

    The output is true.

    See how simple it was. If this helps in your use case, you can use this.

    Using isArray() to check if variable is Array:

    One important thing to note here is that the length proeprty will give an error if it is used on a non-array variable. Hence, the right way to proceed would be to first confirm that the variable on which you are working points to an array, and we can do so using the isArray() function.

    This function will return true or false, depending upon if the variable points to an array or not.

    So in case we are using an if condition to check for empty array, we should do it like this,

    if(Array.isArray(newArr) && newArr.length == 0) {
        console.log("We have an Array and its empty");
    }

    Conclusion:

    So now you understand how to check if an array is empty or not in Javascript. If you have any confusion, feel free to comment below or head to our forum and ask your question there.

    About the author:
    I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight
    Tags:JavascriptArrayHowTo
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS