Determine if an array is empty in JavaScript
This post will discuss how to determine if an array is empty in JavaScript.
To check if an array is empty or not in JavaScript, we can use one of the following functions:
1. Using Array.length property
One common way to check if an array is empty is to use the length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.
|
1 2 3 4 5 6 |
// an empty array let arr = []; let isEmpty = (arr.length === 0); console.log(isEmpty); // true |
It is recommended to use the Array.isArray() function along with the length property. This function determines whether the value passed as an argument is an array or not. It returns true if the value is an array, and false otherwise. We can use this function to check if the value is an array, and then use the Array.length property to check if it is empty. Here’s an example where isArray() function and length property is used together with the AND operator (&&) to determine whether the array exists and is not empty.
|
1 2 3 4 5 6 |
let arr = [1, 2, 3]; if (Array.isArray(arr) && arr.length) { console.log("Array is not empty!"); } else { console.log("Array is empty!"); } |
This approach is recommended as it is simple, reliable, and efficient. However, it does not allow us to define emptiness.
2. Using Array.some() function
Another way to check if an array is empty is to use the Array.some() function. This function executes a callback function for each element in the array, until it finds one that returns true. If no such element is found, the some() function returns false. Therefore, to check if an array is empty or not, we can use a callback function that always returns true, such as function() { return true; }. For example,
|
1 2 3 4 5 6 7 8 9 |
let arr = [1, 2, 3]; if (arr.some(e => true)) { console.log("Array is not empty!"); } else { console.log("Array is empty!"); } // Array is not empty! |
We can use this function to define “emptiness”. For example, to consider an array not-empty only when it contain a non-falsy value, we can do like:
|
1 2 3 4 5 6 7 8 |
let arr = [null, undefined, 0, ""]; if (arr.some(e => e)) { console.log("Array is not empty!"); } else { console.log("Array is empty!"); } // Array is empty! |
This function is not very efficient as it iterates over each element of the array, even if it is not empty. It also requires the array to have a some() function, which may not be the case for some array-like objects.
3. Using Array.toString() function
A third way to check if an array is empty is to use the Array.toString() function. This function converts an array to a string, using a comma as a separator by default. We can use this function to check if the string representation of the array is equal to an empty string or not. Here’s an example:
|
1 2 3 4 5 6 |
let arr = []; if (arr.toString() === "") { console.log("Array is empty!"); } else { console.log("Array is not empty!"); } |
This function does not require any extra iterations or arrays, but it may not be very intuitive or readable.
4. Using logical NOT operator (!)
This operator converts the value to a boolean and returns the opposite value. We can use this operator to check if the array is empty by using its truthy or falsy value. An empty array is falsy, meaning it evaluates to false when converted to a boolean. A non-empty array is truthy, meaning it evaluates to true when converted to a boolean. Here’s an example:
|
1 2 3 4 5 6 |
let arr = [1, 2, 3]; if (Array.isArray(arr) && !arr) { console.log("Array is not empty!"); } else { console.log("Array is empty!"); } |
That’s all about checking if an array is empty in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)