Skip to content

isEmpty

Checks if an array is empty (has no elements). null and undefined are treated as empty arrays and return true.

Available since v2.0.3

import { isEmpty } from '@helpers4/array';
isEmpty(value: readonly unknown[] | null | undefined): value is readonly never[] | null | undefined
ParameterTypeDescription
valuereadonly unknown[] | null | undefinedThe array to check

value is readonly never[] | null | undefined — true if the array has no elements, or if value is null/undefined

Returns true only for arrays with no elements.

isEmpty([])        // => true
isEmpty([1, 2, 3]) // => false
isEmpty([null])    // => false  (null is still an element)

Use isEmpty as an early-return guard for arrays, null, and undefined; the false branch is safely non-empty.

function first<T>(arr: T[] | null | undefined): T | undefined {
  if (isEmpty(arr)) return undefined;
  return arr[0];
}
first([])        // => undefined
first(null)      // => undefined
first([1, 2])    // => 1

View source on GitHub