Array.of(element0, element1, ....)
Parameters present are element0, element1, …. which are basically an element for which the array creation is done.
It simply returns a new Array instance.
Here 2nd column contained int values are versions of the corresponding browser.
Input: Array.of(10, 20, 30) Output: > Array [10, 20, 30]
Here in input arguments of the array.of() function is numbers converted into an array containing the same argument shown in the output.
Input: Array.of("Ram","Geeta") Output: > Array ["Ram", "Geeta"]
Here in input arguments of the array.of() function is string converted into an array containing the same argument shown in the output.
> Array [0, 0, 0] > Array [11, 21, 33] > Array ["Ram", "Geeta"] > Array ["geeksforgeeks"] > Array [2, 3, 4, "Sheeta"]
> Array [Array ["Ram", "Rahim", "Geeta", "Sheeta"]]
Array.of( ) function does not support by Internet Explorer browser. As a developer, it’s your responsibility to provide a code that runs everywhere ( browser in this case ).
So let’s see how to create a polyfill for Array.of( )
- Check if Array.of( ) function is supported in browser or not.
- Now create a function expression named Array.of( ) . This function takes the items of the array.
- Now create an array and push all the argument items into it.
- Now return the array created by you.
> Array.of(1, 2, 3, 4, 5, 6) > [1, 2, 3, 4, 5, 6] > Array.of("John", "Doe", "Smith", "Ram") > ["John", "Doe", "Smith", "Ram"]