Sunday, December 29, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array of() Method

JavaScript Array of() Method

The Javascript array.of() method is an inbuilt method in JavaScript that creates a new array instance with variables present as the argument of the method.

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. 

Example 1: 

Input: Array.of(10, 20, 30)
Output: Array [10, 20, 30]

Explanation: Here in input arguments of the array.of() method is numbers converted into an array containing the same argument shown in the output. 

Example 2:

Input: Array.of("Ram","Geeta")
Output: Array ["Ram", "Geeta"]

Explanation: Here in input arguments of the array.of() method is a string converted into an array containing the same argument shown in the output.

Let’s see the JavaScripts program on the Array.of() method: 

Example 1: In this example, we will see the use of the javascript array.of() method.

JavaScript




//  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('neveropen'));
console.log(Array.of(2, 3, 4, 'Sheeta'));


Output:  

Array [0, 0, 0]
Array [11, 21, 33]
Array ["Ram", "Geeta"]
Array ["neveropen"]
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. 

Example 2: In this example, we will see the use of the javascript array.of() method.

JavaScript




console.log(Array.of(['Ram', 'Rahim', 'Geeta', 'Sheeta']));


Output:  

Array [Array ["Ram", "Rahim", "Geeta", "Sheeta"]]

Polyfill: Polyfills provide a way to implement new features into old browsers that do not support the newest updated version of JavaScript code.

Array.of( ) method does not support by the 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 the Array.of( ) method is supported in the browser or not.
  • Now create a method expression named Array.of( ) . This method 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




// check if Array.of( ) feature present in your browser or not
if (!Array.of) {
 
    // Create a method
    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;
    }
}


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"]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

Supported Browser:

  • Chrome 45 and above
  • Edge 12 and above
  • Firefox 25 and above
  • Opera 26 and above
  • Safari 9 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments