Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

Array.of

Array.of() creates a new array from its arguments, regardless of how many arguments there are or what types they have. A single number is stored as an element, unlike with Array().

Since
ES2015

How it works

Array.of() creates and returns a new Array containing its arguments in order. Unlike the Array constructor, one numeric argument becomes an element rather than an array length; the method can also be inherited by array subclasses or called with another suitable constructor.

Syntax

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

Parameters

NameTypeDescription
element1, …, elementNanyThe values to place into the newly created array, in argument order.

Returns

A new Array instance.

Key points

  • It is a static method that constructs a fresh array from any number of supplied arguments.
  • Each parameter is an element placed into the resulting array.
  • The return value is a newly created Array.
  • A single numeric argument remains an array element instead of being interpreted as a length.
  • Multiple arguments appear in the output array in the same order.
  • Undefined is retained as an actual array element.

Common mistakes

  • Do not substitute Array(7) when you mean an array containing the number 7: Array.of(7) produces [7], while Array(7) creates empty slots and sets length to 7.
  • When inherited by an Array subclass, the result is an instance of that subclass rather than a plain Array.

Example: Creating arrays from arguments

const single = Array.of(7);
const many = Array.of(1, 2, 3);
const empty = Array.of();

console.log(single); // [7]
console.log(many); // [1, 2, 3]
console.log(empty); // []

The method creates arrays whose elements are exactly the supplied arguments, including when there is one argument or none.

When to use it

Consistent array construction

Build an array from a dynamic argument list without special handling for a single numeric value.

Single-value arrays

Create a one-element array containing a number, including zero or another length-like value.

Subclass-aware factories

Use an inherited static factory when an Array subclass should receive the result.

Interview questions

QWhat does Array.of() do?

Array.of() is a static factory that returns a new Array containing all supplied arguments in order. It also supports zero arguments and preserves each argument as an element.

Follow-up: What does Array.of(7) return compared with Array(7)? Array.of(7) returns [7], whereas Array(7) creates an array with length 7 and empty slots.

QHow does Array.of() differ from Array()?

Array.of(7) treats 7 as the first element, so the result is [7]. Array(7) interprets the single numeric argument as the desired length and creates empty slots instead.

Follow-up: What happens with Array.of(undefined)? It returns an array containing one element whose value is undefined.

QWhy is Array.of() described as a generic factory method?

The method is generic and uses its this value as the constructor for the result. An inherited call can therefore create an instance of an Array subclass, provided the constructor accepts the length argument used by the method.

Follow-up: Can any constructor be used? Yes, the documentation says the this value can be any constructor function that accepts one argument representing the new array's length.

  • Array()
  • Array.from()
  • Array.isArray()
  • Array.prototype

Source: Array.of(). Checked 2026-10-15.