In this article, we will check f an array is empty or not in Javascript. We have many methods to do this, some of which are described below.
Methods to Check if an Array is Empty or Not:
- Using array.isArray() method and array.length property.
- By checking the type and length of the array
- Using JavaScript Array.some() method
- Using JavaScript toSrting() method
Method 1: Using array.isArray() method and array.length property
The array can be checked if it is actually an array and if it exists by the Array.isArray() method. This method returns true if the Object passed as a parameter is an array. It also checks for the case if the array is undefined or null.
The array can be checked if it is empty by using the array.length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.
This method and property can be both used together with the AND(&&) operator to determine whether the array exists and is not empty.
Syntax:
Array.isArray(emptyArray) && emptyArray.length
Example: This example shows the above-explained approach.
Javascript
function checkArray() { // Input arrays let emptyArray = []; let nonExistantArray = undefined; let fineArray = [1, 2, 3, 4, 5]; // Checking for type and array length if (Array.isArray(emptyArray) && emptyArray.length) output = true ; else output = false ; // Display output console.log( "Output for emptyArray:" + output); // Checking for type and array length if ( Array.isArray(nonExistantArray) && nonExistantArray.length ) output = true ; else output = false ; // Display output console.log( "Output for nonExistantArray:" + output); // Checking for type and array length if (Array.isArray(fineArray) && fineArray.length) output = true ; else output = false ; // Display output console.log( "Output for fineArray:" + output); } // Function call checkArray(); |
Output for emptyArray:false Output for nonExistantArray:false Output for fineArray:true
Method 2: Checking the type and length of the array
The array can be checked if it exists by checking if the type of the array is ‘undefined’ with the typeof operator. The array is also checked if it is ‘null’. These two things verify that the array exists.
The array can be checked if it is empty by using the array.length property. By checking if the property exists, it can make sure that it is an array, and by checking if the length returned is greater than 0, it can be made sure that the array is not empty.
These properties can then be used together with the AND(&&) operator to determine whether the array exists and is not empty.
Syntax:
typeof emptyArray != "undefined" && emptyArray != null && emptyArray.length != null
&& emptyArray.length > 0
Example:
Javascript
function checkArray() { // Array inputs let emptyArray = []; let nonExistantArray = undefined; let fineArray = [1, 2, 3, 4, 5]; // Checking array type and length if ( typeof emptyArray != "undefined" && emptyArray != null && emptyArray.length != null && emptyArray.length > 0 ) output = true ; else output = false ; // Display output console.log( "Output for emptyArray:" + output); // Checking array type and length if ( typeof nonExistantArray != "undefined" && nonExistantArray != null && nonExistantArray.length != null && nonExistantArray.length > 0 ) output = true ; else output = false ; // Display output console.log( "Output for nonExistantArray:" + output); // Checking array type and length if ( typeof fineArray != "undefined" && fineArray != null && fineArray.length != null && fineArray.length > 0 ) output = true ; else output = false ; // Display output console.log( "Output for fineArray:" + output); } checkArray(); |
Output for emptyArray:false Output for nonExistantArray:false Output for fineArray:true
Method 3: Using JavaScript Array.some() method
The Javascript arr.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method.
Syntax:
arr.some(callback(element,index,array),thisArg)
Example: In this example, we will use JavaScript Array.some() method to check an empty array.
Javascript
function checkArray() { // Array inputs let emptyArray = []; let fineArray = [1, 2, 3, 4, 5]; // Checking array let output = emptyArray.some((element) => true ); // Display output console.log( "Output for emptyArray:" + output); // Checking array output = fineArray.some((element) => true ); // Display output console.log( "Output for fineArray:" + output); } // Function call checkArray(); |
Output for emptyArray:false Output for fineArray:true
Method 4: Using JavaScript toSrting() method
The JavaScript Array toString() Method returns the string representation of the array elements
Syntax:
arr.toString()
Example: In this article, we will use JavaScript toSrting() mehod to convert the given array to string and compare it with an empty string to give output.
Javascript
function checkArray() { // Array inputs let emptyArray = []; let fineArray = [1, 2, 3, 4, 5]; // Checking array let output = emptyArray.toString() === "" ; // Display output console.log( "Output for emptyArray:" + !output); // Checking array output = fineArray.toString() === "" ; // Display output console.log( "Output for fineArray:" + !output); } // Function call checkArray(); |
Output for emptyArray:false Output for fineArray:true
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.