How to Test for an Empty Array With JavaScript
- 1). Create a function that will accept an array as a a parameter and return true if the array is empty or false if the array is not empty. For example, type:
function arrayIsEmpty(array) { - 2). Evaluate the array variable first by itself to avoid a JavaScript error that will occur if you check for an empty array on an undefined or null variable. For example, type:
if (array && - 3). Get the length property of the array, which will be zero if the array if empty, or greater than zero if it is not. For example, type:
if (array && array.length > 0)
return false;
return true;
}
Source...