JavaScript Array.of() function

01 Nov, 2021
The array.of() function is an inbuilt function in JavaScript that creates a new array instance with variables present as the argument of the function. 
Syntax:
Array.of(element0, element1, ....)
Parameters: Parameters present are element0, element1, .... which are basically an element for which the array creation is done. Return Value: It simply returns a new Array instance. Browser Support: Here 2nd column contained int values are versions of the corresponding browser.  
Examples:  
Input: Array.of(10, 20, 30)
Output: > Array [10, 20, 30]
Explanation: 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"]
Explanation: Here in input arguments of the array.of() function is string converted into an array containing the same argument shown in the output.Let’s see JavaScripts program on Array.of() function:  
JavaScript
<script>   //  Here the Array.of() method creates a new Array instance with  // a variable number of arguments, regardless of  // number or type of the arguments.  console.log(Array.of(0, 0, 0));  console.log(Array.of(11, 21, 33));  console.log(Array.of("Ram","Geeta"));  console.log(Array.of('geeksforgeeks'));  console.log(Array.of(2,3,4,'Sheeta'));</script>
Output:  
> Array [0, 0, 0]
> Array [11, 21, 33]
> Array ["Ram", "Geeta"]
> Array ["geeksforgeeks"]
> Array [2, 3, 4, "Sheeta"]
Application: Whenever we need to get elements of an array that time we take the help of the Array.of( ) method in JavaScript.  
JavaScript
<script>  console.log(Array.of(['Ram', 'Rahim', 'Geeta', 'Sheeta']));</script>
Output:  
> Array [Array ["Ram", "Rahim", "Geeta", "Sheeta"]]
Polyfills provide a way to implement new features into old browsers that do not support the newest updated version of JavaScript code.
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( )
Steps :
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.
Javascript
<script> // check if Array.of( ) feature present in your browser or not if(!Array.of){       // Create a function    Array.of = function() {        let newArr = [];                 // Pushing all the arguments into newArr         for(let items in arguments){            newArr.push(arguments[items]);        }        // return the array        return newArr;     } } </script>
Output :
> Array.of(1, 2, 3, 4, 5, 6)
> [1, 2, 3, 4, 5, 6]

> Array.of("John", "Doe", "Smith", "Ram")
> ["John", "Doe", "Smith", "Ram"]
Supported Browser:
Chrome 45 and above
Edge 12 and above
Firefox 25 and above
Opera 26 and above
Safari 9 and above
Akanksha_Rai
_saurabh_jaiswal
sagartomar9927
ysachin2314
javascript-array
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
How to Open URL in New Tab using JavaScript ?
Roadmap to Learn JavaScript For Beginners
How to get character array from string in JavaScript?
How do you run JavaScript script through the Terminal?
JavaScript | console.log() with Examples