JavaScript Array of() Tutorial

In this section, we will learn what the `of()` method is and how we can create an array via this method.

JavaScript Array of() Method

The `Array.of()` method is a static method and is used to create an array containing the values we pass to it as discrete arguments.

Note: the difference between the of() method and `Array` constructor is in the way they handle a single integer argument.

If we pass an integer value to the `of()` method, it will create a single element array. But if we pass the same integer value to the `Array` constructor, it will create an array with the size of the integer value we’ve passed as the argument.

Array of() Syntax:

Array.of()

Array of() Method Parameters:

We can pass any number of values to this method and they become part of the elements of the created array.

Array of() Method Return Value:

The returned value of this method is a new array.

Example: using Array of() method in JavaScript

const arr = Array.of("One","Two","Three","Four","Five");

console.log(arr);

Output:

["One", "Two", "Three", "Four", "Five"]
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies