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.
for/of and for/in loop in JavaScript
Async and Defer attributes in HTML
Defining Functions in JavaScript
Understanding Polyfill JavaScript with window.ShowModalDialog()
undefined and null in JavaScript
JavaScript array.reduce() method
Callback, Promise and Async/Await in JavaScript
What is prototype and prototype chaining in JavaScript ?
How to create an object in JavaScript ?
Javascript array.isarray() method
Javascript array.from() method
Javascript Array Functions
Javascript Code Editor in Google Chrome