Array.from
Creates a new, shallow-copied Array instance from an iterable or array-like object
Syntax
Array.from(arrayLike, mapFn?, thisArg?)Parameters
| Parameter | Type | Description |
|---|---|---|
| arrayLike | Iterable | ArrayLike | An iterable or array-like object to convert |
| mapFn | (element, index) => T | Map function to call on every element |
| thisArg | any | Value to use as this when executing mapFn |
Return Value
A new Array instance
Examples
const str = 'hello';
console.log(Array.from(str)); // ['h', 'e', 'l', 'l', 'o']const range = Array.from({ length: 5 }, (_, i) => i + 1);
console.log(range); // [1, 2, 3, 4, 5]const set = new Set([1, 2, 3, 2, 1]);
const arr = Array.from(set);
console.log(arr); // [1, 2, 3]Understanding Array.from
The Array.from method in JavaScript creates a new, shallow-copied Array instance from an iterable or array-like object. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is Array.from(arrayLike, mapFn?, thisArg?). It accepts 3 parameters: arrayLike, mapFn, thisArg. When called, it returns a new array instance. Understanding when and how to use from() helps you write more expressive, readable code.
Common use cases for Array.from include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-of, array-isarray, array-fill, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.from is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.ofCreates a new Array instance from a variable number of arguments, regardless of number or type of the arguments
Array.isArrayDetermines whether the passed value is an Array
Array.prototype.fillChanges all elements within a range of indices in an array to a static value, returning the modified array
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.