JavaScript function to check array is empty or not17 Mar 2025 | 5 min read JavaScript 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 Output 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 Output 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: Output 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 Output 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 |
Blobs are immutable objects that represent unprocessed data. The file is a Blob derived from data from the file system. Blobs allow us to create file-like objects on the client that we may transfer to APIs, and these APIs expect URLs rather than needing the...
5 min read
In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another...
2 min read
? A JavaScript function is a block of code that consists of a set of instructions to perform a specific task. A function can also be considered as a piece of code that can be used over again and again in the whole program, and it...
5 min read
When the mouse cursor passes on the HTML element, the onmouseover event is worked and shows user-defined functionality. This function also works when the mouse cursor selects the user element, html tag, or required data. When a user moves the cursor away from an element, the...
5 min read
As we know that arrays are helpful in storing data in a sequence in the memory. But sometimes, there may be chances that we may store the same value or duplicate values in an array. Thus, we need to remove such duplicate values from the...
4 min read
? Factorial of number is the product of all positive descending integers. Factorial of n is denoted by n!. For example - 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 Here, 4! is...
3 min read
In order to manage the execution contexts, the JavaScript engine uses a call stack. The working of the JS call stack is performed internally, but we will understand it's working here. In this section, we will discuss the JavaScript call stack, it's working. We will also...
4 min read
The padStart() method returns a result string that achieves a predetermined length by padding a string with the other string starting at the beginning of the string. Once a string reaches the specified length, it can be padded with another string using JavaScript's padStart() method. The...
2 min read
We would have heard and learned the recursion concept when approaching different programming languages. In JavaScript also, we have the concept of Recursion, where we make use of the recursive functions. So, in this section, we are going to learn about Recursion and will also see...
6 min read
JS First Class Function JavaScript is a dynamic scripting language that allows us to use it in the form of an object-oriented style or functional style. In the same way, one such feature of JavaScript is the First class function. In this section, we will discuss the...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India