Skip to content

JavaScript Array from() | Method

  • by

JavaScript Array from() method is used to create 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 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

Your email address will not be published. Required fields are marked *