Skip to content

JavaScript Array from() | Method

  • by

JavaScript Array from() method creates a new array from an array-like or iterable object. This method returns an array from any object with a length property.

Array.from(object, mapFunction, thisValue)

You can only use it as Array.from().

JavaScript Array from()

Simple example code creating a new array from string.

<!DOCTYPE html>
<html>
<body>
  <script>
    var str = "abc";
    let res = Array.from(str);
    
    console.log(res);

  </script>
</body>
</html>

Output:

JavaScript Array from Method

The method with the Mapping Function

console.log(Array.from('foo'));
// output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// output: Array [2, 4, 6]

Do comment if you have any doubts or suggestions on this Js array method tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading