Arrays are the simplest linear data structures that store the same type of data. It can contain all integers, all characters, or all strings as the elements. In this article, we will look into various methods to check whether an array is empty or not in JavaScript.
3 Methods to Check Whether a JavaScript Array is Empty
Checking if a JavaScript array is empty or not is great when dealing with user input. When we need to validate that the received array is not only present but also contains elements. Also, checking emptiness of array is useful when we need to conditional logic, only when array is not empty. Finally, in testing and debugging space, this is an important task to learn for beginners.
Let’s look at methods to check whether an array is empty or not:
1) Using the .length Property
We can use the .length property to check if a JavaScript array is empty or not. It can check the length of the array specifies the number of elements that it contains. If the length is zero, it means that the array has no elements, i.e. the array is empty.
Let’s see how to do it:
//declaring an empty array let arr=[]; //checking the length if(arr.length==0){ console.log("The array is empty"); } else{ console.log("The array is not empty"); }
Output:
The array is empty
In this example, we have taken an array with no elements. Hence, the length of the array is zero. As the array’s length is zero, this means that the array is empty. We have obtained the desired result.
Let us take another example:
//declaring an empty array let arr=[1,2,3]; //checking the length if(arr.length==0){ console.log("The array is empty"); } else{ console.log("The array is not empty"); }
Output:
The array is not empty
In this example, we have taken an array with three elements. Hence, the length of the array is three. As the array’s length is three, this means that the array is not empty. We have obtained the desired result.
The length property is also good to get the last element of the array in JavaScript.
2) Using the Array.isArray() Method
Another method to check if a JavaScript array is empty is to combine the Array.isArray() method and the length method. Array.isArray() method checks if the variable is an array. So, we combine both methods. If both conditions are true, i.e. the variable is an array and its length is zero then the array is empty. Otherwise, the array is not empty.
Let us take an example.:
//declaring an empty array let arr=[]; //checking if the variable is an array and the length of the array if(Array.isArray(arr) && arr.length==0){ console.log("The array is empty."); } else{ console.log("The array is not empty."); }
Output:
The array is empty.
In this example, we check both conditions. One is to check if the variable is an array and the other is to check if the length is zero. The variable arr is an array and its length is zero. Thus, we have obtained the desired result.
Let us take another example:
//declaring a variable let str="FavTutor"; //checking if the variable is an array and the length of the array if(Array.isArray(str) && str.length==0){ console.log("This is an array and it is empty."); } else{ console.log("This is not an array or is not empty."); }
Output:
This is not an array or is not empty.
In this example, we have taken a string, not an array. Hence, we have got the desired output.
3) Using the toString() Method
The toString method in JavaScript converts an array to a string. If the array is empty, the string representation would be an empty string, i.e.(“”).
The syntax of the toString() method is as follows:
let convertedStr = arr.toString()
where, ‘convertedStr’ is the variable to store the string that we get from the toString() function, and ‘arr’ variable represents the array that has to be converted to a string.
Here is an example:
//declaring an array let arr=[]; //using the toString() method let str=arr.toString(); //checking if array is empty or not if(str==""){ console.log("The array is empty."); } else{ console.log("The array is not empty."); }
Output:
The array is empty.
In this example, we convert the array arr to a string. The converted string becomes “ ”, i.e. an empty string. Thus, it is proved that the array is empty. We have obtained the desired result.
Conclusion
In this article, we dive deep into various methods to check whether a JavaScript array is empty or not. We used the .length method, the Array.isArray() method, the Array.prototype.every() method and Array.prototype.every() method to check the emptiness of an array.