Skip to content

isArrayLike

Checks if a value is array-like: has a non-negative integer length property.

Returns true for arrays, strings, arguments objects, NodeList, typed arrays, and any object with a valid length. Functions are excluded even though they have a length (arity), as they are not considered array-like in practice.

Available since v2.0.3

import { isArrayLike } from '@helpers4/guard';
isArrayLike(value: unknown): value is ArrayLike<unknown>
ParameterTypeDescription
valueunknownThe value to check

value is ArrayLike<unknown> — true if value is array-like

Arrays, strings, and objects with a non-negative integer length are array-like.

isArrayLike([1, 2, 3])       // => true
isArrayLike('hello')         // => true
isArrayLike({ length: 3 })   // => true
isArrayLike({ length: -1 })  // => false
isArrayLike(() => {})        // => false  (functions excluded)
isArrayLike(null)            // => false

Use as a guard before Array.from().

function toArray(value: unknown): unknown[] {
  if (isArrayLike(value)) return Array.from(value);
  return [value];
}
toArray([1, 2])       // => [1, 2]
toArray('abc')        // => ['a', 'b', 'c']
toArray(42)           // => [42]

View source on GitHub