Value is array-like

OSCC · JavaScript, Type, Array · Oct 20, 2020

Checks if the provided argument is array-like (i.e. is iterable).

  • Check if the provided argument is not null and that its Symbol.iterator property is a function.
const isArrayLike = obj =>
  obj != null && typeof obj[Symbol.iterator] === 'function';
isArrayLike([1, 2, 3]); // true
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false

Recommended snippets

  • Checks if the provided value is of the specified type.

  • Collection is empty

    JavaScript, Type

    Checks if the a value is an empty object/collection, has no enumerable properties or is any type that is not considered a collection.

  • Value is async function

    JavaScript, Type

    Checks if the given argument is an async function.