Below is the example of the Array from() method.
- Example:
<script>document.write(Array.from("This is JavaScript Array "+"from() Method"));</script>chevron_rightfilter_none - Output:
T,h,i,s, ,i,s, ,J,a,v,a,S,c,r,i,p,t, ,A,r,r,a,y, ,f,r,o,m,(,), ,M,e,t,h,o,d
The arr.from() method is used to creates a new array instance from a given array. In the case of a string, every alphabet of the string is converted to an element of the new array instance and in case of integer values, a new array instance simply takes the elements of the given array.
Syntax:
Array.from(object, mapFunction, thisValue)
Parameters: This method accepts three parameters as mentioned above and described below:
- object: This pareter holds that object that will convert into an array
- mapFunction: This parameter is optional used to call on each item of the array.
- thisValue: This parameter is optional, it holds the context to be passed as this to be used while executing the mapFunction. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.
Return value: It returns a new Array instance whose elements are same as the given array. In the case of a string, every alphabet of the string is converted to an element of the new array instance.
Below example illustrate the Array from() method in JavaScript:
- Example 1: Here as we see that output creates a new array whose content is the same as input in case of a integer.
Input : 10, 20, 30 Output: Array [10, 20, 30]
- Example 2: Here as we see that output creates a new array whose content is the same as input every alphabet of the string is converted to an element of the new array instance.
Input : geeksforgeeks Output: Array ["g", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"]
Code for the above method is provided below:
Program 1:
<script> document.write(Array.from("GeeksforGeeks")); document.write("<br />") document.write(Array.from([10, 20, 30])); </script> |
Output:
G,e,e,k,s,f,o,r,G,e,e,k,s 10,20,30
Program 2:
<script> // Here input array is [1,2,3] and output // become bouble of each elements. document.write(Array.from([1, 2, 3], x => x + x)); </script> |
Output:
2,4,6
Note: If we take a complex number as the parameter, it returns an error because only array and string can be taken as the parameter.
Supported Browsers: The browsers supported by JavaScript Array from() method are listed below:
- Google Chrome 45.0
- Microsoft Edge 12.0
- Mozilla Firefox 32.0
- Safari 9.0
- Opera 25.0
Recommended Posts:
- JavaScript | Array from() Method
- JavaScript Array map() Method
- JavaScript Array every() Method
- JavaScript Array pop() Method
- JavaScript | Array map() Method
- JavaScript Array some() Method
- JavaScript Array keys() Method
- JavaScript Array slice() Method
- JavaScript Array fill() Method
- JavaScript Array lastIndexOf() Method
- JavaScript Array valueOf() Method
- JavaScript Array copyWithin() Method
- JavaScript Array reduce() Method
- JavaScript Array flat() Method
- JavaScript Array join() Method
- JavaScript Array indexOf() Method
- JavaScript Array unshift() Method
- JavaScript Array isArray() Method
- JavaScript Array toString() Method
- JavaScript Array sort() Method
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.


