JavaScript Array of() Method

JavaScript Array of() Method creates and returns a new array from different number of arguments. It does not focus on the type and number of arguments.

In this tutorial, you will learn how to improve array construction using the JavaScript Array.of() method in ES6.

Syntax

Array.of(element1, element2,.....)  

Parameters

Elements used to create the array.

Return Value

It returns a newly created array instance.

Examples

Array.of(1);         // [1]
Array.of(1, 2, 3);   // [1, 2, 3]
Array.of(undefined); // [undefined]

Similar to Array, it also provides a constructor that handles the integer arguments, but in a different manner.

Array.of(7); // [7]
Array(7); // array of 7 empty slots

Array.of(1, 2, 3); // [1, 2, 3]
Array(1, 2, 3);    // [1, 2, 3]