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 |
In this article, we are going to discuss about the formatting of numbers with commas in JavaScript. Sometimes, we require formatting a number with commas in the HTML page in order to make it easy to read. Using JavaScript, we can transform a number into...
4 min read
The mousedown event uses the mouse movement on the web page using the javascript functionality. The mouse-down event works on the laptop's mouse, or single mouse click. If we press the on a laptop, the mouse down event starts its handling functionality. Syntax The following syntax shows...
3 min read
Base64 is a binary-to-text encoding system that converts binary data into a radix-64 representation and then encodes it as an ASCII string. It is frequently used to store data in files and send data over the internet. With native JavaScript functions and modules, Base64 data...
3 min read
Javascript logical assignment operators help to use the logical operators with the default and another expression. It works between two expressions or values with a single operator. ES2021 provides three logical assignment operators, which are as follows: Logical AND assignment operator Logical OR assignment operator Nullish coalescing assignment...
5 min read
JavaScript is an asynchronous (non-blocking) and single-threaded programming language, meaning only one process can be run at a time. In programming languages, callback hell generally refers to an ineffective way of writing code with asynchronous calls. It is also known as the Pyramid of Doom. The callback...
12 min read
Window.location is used to provide a Location object with details about the document's current location. This Location object reflects the location (URL) of the object it's linked to, i.e., it holds information about the current content location (host, href, etc.) JavaScript Location Object The window.location attribute represents...
8 min read
What is an URL? A URL is another name for a web address. For instance, a word-based URL is TpointTech.com. A URL can also be created using an IP address (ex. 192.168.2.24). Most consumers provide the name's address when conducting an online search because names are...
3 min read
We will understand how to manage <select> option in JavaScript in this tutorial. HTML Select Option A <select> option facilitate us the options list. It permits us to choose a single or more than one option. We use the <option> and <select> elements for forming a <select>...
4 min read
? A JSON refers to the JavaScript Object Notation format used to store simple objects and data structures. Usually, JSON files are backup files, which is used to take backup of data that restored back to the application when needed. In early days, JSON files was used...
8 min read
The JavaScript change event is an event type that gets executed when the focus on an element is changed. The change event of JavaScript inherits all the methods and properties of the Event. Here, in this section, we will understand and implement the practical use of...
5 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