JavaScript function to check array is empty or notJavaScript provides in-built functions to check whether the array is empty or not. Following are the method offered by JavaScript programming to check an empty array:
The Array.isArray() function checks the array type (passed parameter is an array or not) and array.length find the length of the array. So, we can identify the empty array easily. You can use them individually and separately as well. Now, we will learn these methods in detail with examples: .length propertyThe 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., arr1.length. SyntaxIf the length returned by this property is 0, it refers to true means the array is empty. Otherwise, the array is not empty if it returns a non-zero value. How to use?It is directly used with a user-defined array concatenated by dot (.) operator. See the below example of this property to understand it better. Copy Code Test it NowOutput In the below output, you can see that the first array named arr1 is not empty as it has five elements in it, whereas the second array named arr2 is empty. arr1 is not empty arr2 is empty Array.isArray()In JavaScript, arrays not actually array; they are objects. So, if you check the type of array using typeof property, it will return value as an object. But now we have Array.isArray() function to check the type of array, which can be used with .length property to check empty array. This method helps to determine that the value you have passed in this function is array or not. We can say that it identifies the array type or normal data type variable. It can also determine the undefined or null array. SyntaxIt returns a Boolean value, either true or false. Return valuesTrue - If it returns true, the passed value is an array. False - If it returns false, the value passed in this function is not an array type. How to use?This function is used with name of array, e.g., Array.isArray(arr1). Here, we will use this function with .length property to check the empty array. See the implementation of this function in a JavaScript example to understand it better. ExampleCopy Code Test it NowOutput In the below output, you can see that the value we have checked using Array.isArray() function is an array, but the array is not empty. arr1 is an array but it is not empty. Example 2In this example, we will check an array for empty and another variable for non-array value. See the code below: Test it NowOutput In the below output, you can see that the first array named arr1 an array and it is empty as well because it does not have elements in it, whereas the second variable named arr2 is not an array. arr1 is an array and it is empty as well. arr2 is not an array. isArray() and .length property togetherBoth length property and Array.isArray() function can be used together inside the if-condition connected by AND (&&) operator. SyntaxFollowing is the syntax for both isArray() and length property how they used together: Use the above syntax with if-else condition to check the array type and empty array. How to use it?See the below example to understand how both functions work together to check an empty array in JavaScript. Check if array is Empty You can use OR (||) operator between both conditions to check array is empty. Check if array is not Empty You can use the inverse method to check array is not empty. For this, use AND (&&) operator to put the conditions to check array is not empty. Example 1Now, we will use this function in an example to understand it better. In this example, we have checked two variables, either they are array type or not. Copy Code Test it NowOutput You can see that arr1 is empty array and err2 is either not an array or not empty in the below output. arr1 is an array and it is empty. Either arr2 is not an array or it is not empty. Note: It is a complex and less clear way to get the exact result.Array.isArray() vs .lengthThe .length property can be used with other data types like string to find the length. Whereas the Array.isArray() method is only used with array data, which also helps to identify that your array is an array. Array.isArray() method is a bit lengthy because here we first to determine whether the variable is array type or not and then we have to use .length property to check empty array. So, we will suggest you use .length property to directly check the length of the array variable for small calculation and determine whether it is empty or not. Next TopicJavaScript multi-line String |