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:
Array.of()
This static-method creates a new empty array.
Returns value of type Array
.
Array.of(element1)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
This static-method creates a new array with one element.
Returns value of type Array
.
Array.of(element1, element2)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
This static-method creates a new array with two elements.
Returns value of type Array
.
Array.of(element1, element2, /* …, */ elementN)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
element1 | required | The first element to include in the array. |
element2 | required | The second element to include in the array. |
elementN | optional | Additional 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,
- We use the Array.of() method with no arguments to create a new empty array.
- The result is stored in the variable emptyArray.
- 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,
- We use the Array.of() method with one argument, 1, to create a new array with one element.
- The result is stored in the variable singleElementArray.
- 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,
- We use the Array.of() method with multiple arguments, 1, 2, and 3, to create a new array with three elements.
- The result is stored in the variable multipleElementsArray.
- 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.