BlogsDope image BlogsDope

Check if an array is empty in JavaScript

March 3, 2021 ARRAY JAVASCRIPT 5191

JavaScript is the one the most popular language in the world of web development. And when you're programming in JavaScript, you would have surely worked with arrays, and in that case you might need to know how to check whether an array is empty or not.

You will find it helpful because you may end up in a position where you want to disable a button whenever the array is empty and enable it if the array is not empty, there are many more such scenarios of it where you will find it helpful.

Well, how do we get to know if an array is empty or not, we just check if there are any element in it or not, if there are no element then surely the array is empty.

The real strength of JavaScript arrays are the built-in array properties and methods. And to check if an array is empty or not, we can use length property.

The length property returns the length of the array by which you can determine whether the array is empty or not. This property is directly used with the name of array concatenated by dot (.) operator, e.g. arr.length.

But there is one problem with length property that it can be used on other data types too. For example, if there is an empty string str = " ", then if we try the length function str.length the answer returned will be 0 i.e. empty.

So, it will be better if we also check that your array is an actual array or not. Well, for this also JavaScript has something in it's pocket. We can use the Array.isArray() method to confirm it. This method helps to determine that the value you have passed in this function is array or not. So, this will help us in identifying the array.

Approach

  1. Check if the given variable is an array or not.
  2. If above step is true, then find the length of the array else print given variable is not an array.
  3. Check if the length of the array 0 or greater than 0.
  4. If the value is 0 print array is empty, or else print array is not empty.

Code

//JavaScript Function 
function isEmptyArray(arr){
if(Array.isArray(arr))
  {
    if(arr.length === 0)
      {
        document.write("Given Array is empty<br>"); //if array is an array and array is empty condition
      }
    else
      {
        document.write("Given array is not empty<br>"); // if array is an array but not empty condition
      }
  }
else
  {
  document.write("Given variable is not an array<br>"); //if given array is not an array condition
  }
}
//checking all three conditions
var arr1 = [10,20,30]; 
isEmptyArray(arr1);
var arr2 = [];
isEmptyArray(arr2);
var string = "CodesDope";
isEmptyArray(string);

Output : Given array is not empty 
         Given Array is empty
         Given variable is not an array

Well, you can check if the array is empty or not in just one line of code. All you have to do it to write all of these a single line using && (and) logical operator. It is shown below.

if(Array.isArray(arr1) == true && arr1.length == 0) if this holds true then the given input is an array and it is also empty, else the input is not an array or input is an array but not empty. 


Liked the post?
Hi, I am Priyansh Jha. I am a pre-final year student of Electronics and Communication major undergraduate.
Editor's Picks
0 COMMENT

Please login to view or add comment(s).