Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.from: an array from iterable or array-like input

Array.from creates a new array from an iterable or indexed array-like object and can map each value during construction.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

Array.from(items[, mapFn[, thisArg]])

Array.from(items, mapFn, thisArg) uses the iteration protocol when items provides an iterator; otherwise it reads length and indexed properties from an array-like object. It always returns a new result. Optional mapFn(value, index) runs as values are created and thisArg supplies its this.

A missing array-like index is read as undefined, so the result is dense rather than sparse. A string iterator advances by Unicode code points, handling surrogate pairs better than splitting UTF-16 code units. null or undefined input fails, and a supplied non-callable mapFn throws TypeError. Another primitive without an iterator can be treated as array-like with length zero and yield an empty array; failures from a supplied iterator propagate.

The method is generic over the constructor used as its this, while ordinary Array.from constructs an Array. Array-like length is clamped to a safe integer range. The example exposes a missing index and maps it without an intermediate array.

Examples

const source = {0: "a", 2: "c", length: 3};
const result = Array.from(source, (value, index) => value ?? `gap${index}`);
console.log(result.join("|"));
console.log(result !== source);

Result

a|gap1|c
true

Sources