Array.from() / Array.isArray() Since: ES6(ECMAScript 2015)
Static methods for creating and checking arrays. Array.from() creates an array from an array-like object, and Array.isArray() checks whether a value is an array.
Syntax
// Creates an array from an array-like object or iterable. var arr = Array.from(arrayLike, mappingFunction); // Checks whether a value is an array. var result = Array.isArray(value); // Creates an array using the given arguments as elements. var arr = Array.of(element1, element2, ...);
Method List
| Method | Description |
|---|---|
| Array.from(target, function) | Creates a new array from an array-like object or iterable. If you pass a mapping function as the second argument, each element is transformed as the array is built. |
| Array.isArray(value) | Returns true if the specified value is an array, or false otherwise. |
| Array.of(element1, element2, ...) | Creates a new array using the given arguments as its elements. Unlike new Array(), passing a single number does not set the array length. |
Sample Code
// Creates an array from a string.
var chars = Array.from("Hello");
console.log(chars); // Outputs 'H,e,l,l,o'.
// Converts a NodeList to an array.
var items = document.querySelectorAll("li");
var arr = Array.from(items);
console.log(Array.isArray(arr)); // Outputs 'true'.
// Creates an array while transforming elements with a mapping function.
var numbers = Array.from([1, 2, 3], function(n) {
return n * 10;
});
console.log(numbers); // Outputs '10,20,30'.
// Creates an array of sequential numbers.
var range = Array.from({ length: 5 }, function(_, i) {
return i + 1;
});
console.log(range); // Outputs '1,2,3,4,5'.
// Checks the type with Array.isArray.
console.log(Array.isArray([1, 2, 3])); // Outputs 'true'.
console.log(Array.isArray("abc")); // Outputs 'false'.
console.log(Array.isArray({ length: 3 })); // Outputs 'false'.
// Creates an array with Array.of().
console.log(Array.of(3)); // Outputs '3'. An array containing the element 3.
console.log(new Array(3)); // Outputs ',,'. An empty array with a length of 3.
Overview
Array.from(), Array.isArray(), and Array.of() are all static methods on the Array constructor. They are called directly on Array rather than on an instance, which distinguishes them from instance methods like array.push() or array.map().
Array.from() is especially useful for converting objects that resemble arrays but lack array methods — such as the NodeList returned by document.querySelectorAll() — into real arrays. Once converted, all array methods become available, including array.forEach() and array.filter(). The technique of passing { length: n } to generate a sequential array is also commonly used in practice.
Array.isArray() is the only reliable way to check whether a value is an array. typeof [] returns "object", so typeof cannot distinguish between arrays and plain objects. Always use Array.isArray() when you need to check for an array.
Browser Compatibility
44 or earlier ×
31 or earlier ×
Android Browser
50+ ○
44 or earlier ×
Chrome Android
50+ ○
44 or earlier ×
Firefox Android
79+ ○
31 or earlier ×If you find any errors or copyright issues, please contact us.