JavaScript Array.from(), Array.of(), and Array.isArray() - Static Methods

Master the three static Array methods: Array.from() for creating arrays from iterables, Array.of() for safe creation, and Array.isArray() for type checking. Free reference.

Static Methods

Detailed Explanation

Static Array Methods

These methods live on the Array constructor itself, not on array instances.

Array.from()

Creates a new array from any iterable or array-like object:

// From a string
Array.from("hello"); // ["h", "e", "l", "l", "o"]

// From a Set (removes duplicates)
Array.from(new Set([1, 2, 2, 3, 3])); // [1, 2, 3]

// From a Map
Array.from(new Map([["a", 1], ["b", 2]]));
// [["a", 1], ["b", 2]]

// From a NodeList
Array.from(document.querySelectorAll(".item"));

With a map function (second argument):

// Create array of specific length with computed values
Array.from({ length: 5 }, (_, i) => i + 1);
// [1, 2, 3, 4, 5]

// Generate a range
Array.from({ length: 10 }, (_, i) => i * 10);
// [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]

Array.of()

Creates an array from its arguments, without the Array constructor ambiguity:

Array.of(7);       // [7] — one element: 7
Array(7);          // [empty x 7] — sparse array of length 7!

Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]

In practice, array literals [7] are preferred. Array.of() is mainly useful when the arguments are dynamic and you want to avoid the constructor pitfall.

Array.isArray()

The most reliable way to check if a value is an array:

Array.isArray([1, 2, 3]);        // true
Array.isArray("string");          // false
Array.isArray({ length: 3 });     // false
Array.isArray(new Array(3));      // true
Array.isArray(new Uint8Array(3)); // false (typed arrays are not Array)

Why not typeof or instanceof?

  • typeof [] returns "object" (not useful)
  • [] instanceof Array fails across iframes/realms
  • Array.isArray() works correctly in all cases

Common Pattern: Normalize Input

function ensureArray(input) {
  if (Array.isArray(input)) return input;
  return [input]; // wrap single value
}
ensureArray("hello");  // ["hello"]
ensureArray([1, 2]);   // [1, 2]

Use Case

Use Array.from() to convert DOM NodeLists, Sets, Maps, and strings to arrays, or to generate sequences. Use Array.isArray() for type checking in utility functions and API input validation. Array.of() is rarely needed but avoids the Array constructor ambiguity.

Try It — JavaScript Array Methods Reference

Open full tool