JavaScript

How to check if an array is empty in JavaScript

Knowing how to check for an empty array is an important coding skill which can often come in handy. It can be helpful in a situation where you have to show or hide something on a web page depending on whether the array is empty or not.

Similarly, there are many other places where you will find this skill helpful. The purpose of this post is to explain the code, the concept behind the code and the most common use cases of checking for empty arrays in JavaScript code. So let’s get started

How to check for an empty array

Checking for an empty array in JavaScript code is very simple, javaScript provides a simple length() method which helps to know the total number of elements of an array. If the length() method returns 0 then that means that an array is empty:

varemptyArray = [];

if (emptyArray.length === 0) {
console.log('The array is empty.');
}
else
{
console.log('The array has at least one or more elements.');
}

If we fill the array with elements then:

varnotAnEmptyArray = [1, 2, 3, 4, 5];

if (notAnEmptyArray.length === 0) {
console.log('The array is empty.');
}
else
{
console.log('The array has at least one or more elements.');
}

The thing about the .length method is that it can work with data types other than arrays:

varnotAnArray = 'This is not an array.';

if (notAnArray.length === 0) {
console.log('The array is empty.');
}
else
{
console.log('The array has at least one or more elements.');
}

As you can see in the example above, the code was interpreted by the browser without any errors even though there was no array present in the code. So if we are unsure about the data type of our variable, we might first want to check whether it’s an array or some other variable. For this purpose we will not use typeof operator as arrays in JavaScript are instances of objects and their data types are objects. Rather we will use the Array.isArray() method:

varnotAnArray = 'This is not an array.';

if (Array.isArray(notAnArray)) {

if (notAnArray.length === 0) {
console.log('The array is empty.');
  }
else
  {
console.log('The array has at least one or more elements.');
  }

}
else
{
console.log('The given variable type is not array.')
}

If we change the variable to an empty array:

varnotAnArray = [];

if (Array.isArray(notAnArray)) {

if (notAnArray.length === 0) {
console.log('The array is empty.');
  }
else
  {
console.log('The array has at least one or more elements.');
  }

}
else
{
console.log('The given variable type is not array.')
}

Conclusion

Checking whether an array is empty or not is a kind of a coding problem that can often be asked in quizzes and exams. In this post we learned to use the length() method to know if an array is empty or not.

The process of checking whether an array is empty generally consists of two steps. The first step is to know if the variable’s type is array type or not. The second step is to know the total number of array elements using the length() method; if the length is equal to 0 then the array is empty; otherwise if it is greater than 0 then the array has some elements in it.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.