Feel free to copy and paste it into your project if you’re looking for a quick and easy solution:
Firstly, it checks to see if the variable is an array by using the Array.isArray() method.
If the variable in question is undefined or if it is another variable type such as a string or an object, the function will return TRUE.
Once the function has confirmed that it is dealing with an actual array, it checks the Array.length property. If the length of the object is 0, then our function will presume that the array is empty and return a TRUE value.
Finally, if it passes the two checks above, then our arrayIsEmpty function will return FALSE.
Take a look at the following example of the function in action:
As you can see, arrayIsEmpty returns FALSE for the first array, as it is definitely not empty. However, it returns TRUE for the second array.
And that’s it! As you can see, it’s actually pretty simple.
All you have to do is make sure that the variable is an actual array and that its length property is equal to 0.