Javascript array.of() method

Javascript array.of() method

This is another static method in the Array class just like Array.from() and Array.isArray(). This method is used to construct an array of the values passed to the method as parameters.

Please find some of the examples mentioned below :-

console.log(Array.of(1,2,3,4,)) //[1,2,3,4]
console.log(Array.of("John")) // ["John"]
console.log(Array.of("John","Doe")) // ["John", "Doe"]
console.log(Array.of({name:"Foo",Age:21},{name:"Bar",Age:24}))//[{…}, {…}]
console.log(Array.of('<','.','/'))//["<", ".", "/"]
console.log(Array.of(5)) //[5]
let arr = new Array(5)
console.log(arr) //(5) [empty × 5]

The important point is to note the difference between the output of the Array.of(n) and Array(n) where n is the integer value passed as a parameter. The Array.of() method creates an Array with the single element while the Array(n) method constructs an array of n number of elements i.e equal to the integral value passed at parameter with empty values.