JavaScript Tutorials

JavaScript Array.of()
Syntax & Examples

Array.of() static-method

The Array.of() method in JavaScript creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.


Syntax of Array.of()

There are 4 variations for the syntax of Array.of() static-method. They are:

1.
Array.of()

This static-method creates a new empty array.

Returns value of type Array.

2.
Array.of(element1)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.

This static-method creates a new array with one element.

Returns value of type Array.

3.
Array.of(element1, element2)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.

This static-method creates a new array with two elements.

Returns value of type Array.

4.
Array.of(element1, element2, /* …, */ elementN)

Parameters

ParameterOptional/RequiredDescription
element1requiredThe first element to include in the array.
element2requiredThe second element to include in the array.
elementNoptionalAdditional elements to include in the array.

This static-method creates a new array with one or more elements.

Returns value of type Array.



✐ Examples

1 Using Array.of() method to create an empty array

In JavaScript, we can use the Array.of() method to create a new empty array.

For example,

  1. We use the Array.of() method with no arguments to create a new empty array.
  2. The result is stored in the variable emptyArray.
  3. We log emptyArray to the console using console.log() method to see the created array.

JavaScript Program

const emptyArray = Array.of();
console.log(emptyArray);

Output

[]

2 Using Array.of() method to create an array with one element

We can use the Array.of() method to create a new array with one element.

For example,

  1. We use the Array.of() method with one argument, 1, to create a new array with one element.
  2. The result is stored in the variable singleElementArray.
  3. We log singleElementArray to the console using console.log() method to see the created array.

JavaScript Program

const singleElementArray = Array.of(1);
console.log(singleElementArray);

Output

[1]

3 Using Array.of() method to create an array with multiple elements

We can use the Array.of() method to create a new array with multiple elements.

For example,

  1. We use the Array.of() method with multiple arguments, 1, 2, and 3, to create a new array with three elements.
  2. The result is stored in the variable multipleElementsArray.
  3. We log multipleElementsArray to the console using console.log() method to see the created array.

JavaScript Program

const multipleElementsArray = Array.of(1, 2, 3);
console.log(multipleElementsArray);

Output

[1, 2, 3]

Summary

In this JavaScript tutorial, we learned about of() static-method of Array: the syntax and few working examples with output and detailed explanation for each example.