Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Array.of

Creates an array from its arguments.

Available since: ES2015

Syntax

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

Parameters

ParameterTypeDescription
element1, …anyThe elements of the new array.

Return value

ArrayA new array of the given elements.

Examples

The difference between Array.of and new Array with one number:

console.log(Array.of(3).length);
console.log(new Array(3).length);

Output:

1
3

Notes

new Array(3) creates an empty array of length 3, while Array.of(3) creates an array with one element, 3. Usually the simplest is the literal [3].

See also

In the tutorial

  • Arrays — What an array is in JavaScript: creating one, indexes, length, changing elements and nested arrays.

Thanks for reading!

Donate to the site — we work hard for you.