Check Array Is Empty in JavaScript Example

Check Array Is Empty in JavaScript Example | To further clarify the code and philosophy underlying famous use cases, we continue with Flexiple’s instructional series. In this article, we’ll provide a solution for a particular topic: Javascript can be used to determine if an array is empty.

Where might we apply this? This could be helpful if you want to run a specific script when the array is empty, for example, to enable or disable buttons based on whether or not the necessary field has any input.

We advise reading the full post if you are new to programming or are unfamiliar with javascript because each section will be helpful. But if all you need is the code, you may quickly look at the section below.

Check Array Is Empty in JavaScript Using array.isArray() method

We’ll quickly go over the code and its demonstration to determine whether or not an array is empty and to explain the use of these particular functions.

JavaScript code to see if the given array is empty or not using the isArray() method

// To check if an array is empty using javascript
function arrayIsEmpty(array) {
  // If it's not an array, return FALSE.
  if (!Array.isArray(array)) {
    return FALSE;
  }
  // If it is an array, check its length property
  if (array.length == 0) {
    // Return TRUE if the array is empty
    return true;
  }
  // Otherwise, return FALSE.
  return false;
}

var carArr = new Array("Ford", "Honda", "BMW");
// An example of a JavaScript array that is empty.
var arrTwo = new Array();

console.log(arrayIsEmpty(carArr)); // false
console.log(arrayIsEmpty(arrTwo)); // true

Output:-

false
true

First, we use the Array.isArray() method to determine whether a variable is an array. The variable will go to the else condition if the given variable is an array, in which case the condition !Array.isArray() will return False.

The function will return False if the supplied variable is something other than an array, such as undefined or another variable type, like a string or object. Now that we’ve established that the variable is an array, we can use the Array.length property to determine how long the array is. The array is considered empty, and the function returns TRUE if the object’s length is zero. Otherwise, the function will return false because the array is not empty.

Here, you can see that carArr is an array and meets the second requirement to determine whether the array’s length is empty. The function returns False since the array is not empty because it contains three elements. Since it is an array once more in the second scenario, arrTwo, it meets the second requirement. The function, in this case, returns True because the array is empty.

Check Array Is Empty in JavaScript using array.length() Method

We may determine whether an array is empty by combining the length property and JavaScript’s logical “not” operator, the “!” sign. An expression is negated using the ! operator. In other words, if an array is empty, we can utilize it to return true.

Make an array first, but without any elements. Once we are confident that we are only working with an array, we can quickly determine whether or not the array is empty by utilizing the length property. The array is empty if the length of the array is 0, else it is not empty.

var carArr = new Array("Ford", "Honda", "BMW");
var arrTwo = new Array();

console.log(!carArr.length); // false
console.log(!arrTwo.length); // true

Output:-

false
true

One obvious question is, why not use the length property right away? Will it not simplify the code? True, but before utilizing the length property, we need to ensure we’re working with an array because it can also indicate that a variable is empty for non-array variables.

Conclusion

In this post, we learned how to use JavaScript’s length property in various ways to determine whether the given array is empty in JavaScript or not. The length attribute gives the array’s total number of entries.

We also discovered that it is better to check whether the provided value is an array as expected when using the Array.isArray function in conjunction with the.length property.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *