JavaScript | Array from() Method

The JavaScript Array from() method returns an Array object from any object with a length property or an iterable object.

Syntax :

Array.from(object, mapFunction, thisValue)

Parameter :



  • object : This Parameter is required to specify the object to convert to an array.
  • mapFunction : This Parameter specify the map function to call on each item of the array.
  • currentIndex : This Parameter specify the array index of the current element.
  • thisValue : This Parameter specify the value to use as this when executing the mapFunction.

Return Value: It returns an array object.

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h2>GeeksForGeeks
      </h2>
        <p>Create an Array
          from a String:</p>
        <p id="demo"></p>
        <script>
            var myArr = Array.from(
              "GeeksForGeeks");
            document.getElementById(
              "demo").innerHTML = myArr;
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h2>GeeksForGeeks</h2>
        <p>Create an Array from 
          a String: "45878965412365"
      </p>
        <p id="demo"></p>
        <script>
            var myArr = 
                Array.from("45878965412365");
            
            document.getElementById(
              "demo").innerHTML = myArr;
        </script>
    </center>
</body>
  
</html>

chevron_right


Output :

Supported Browsers:

  • Google Chrome
  • Mozilla Firefox
  • Edge
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.