Javascript empty array check

You want to do the check for is array first. If you do it the other way round, it will generate an error if the array is undefined.
After, you check the length of the array to check empty.

if (Array.isArray(array) && array.length > 0) {
  // if it is not an array and is not empty
} else {
  // is empty
}

– isArray: If the value is an Arraytrue is returned; otherwise, false is.

– The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.

Add a Comment

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