The standard way to create an array in JavaScript is an array literal: const fruits = ["Apple", "Banana", "Cherry"];. You can also use Array.from(), Array.of(), the spread operator or the Array() constructor, which are useful when you build an array from another collection or need a fixed length.
This answer covers each method, the traps of new Array(n) and how to copy and transform arrays with the methods available in current JavaScript.
A literal is the shortest, fastest to read and most common form. Elements can be of any type, and the length follows the number of elements.
Use const for the variable. It does not make the array immutable, only the binding: you can still push and change elements, but you cannot assign a new array to fruits. at(-1) reads from the end and is supported in all current browsers and runtimes.
new Array(...) behaves differently depending on its arguments.
A single number creates a sparse array with that length and no elements. Those holes are skipped by forEach, map and most iteration methods, which surprises many developers.
Prefer a literal for known values, and Array.from or fill when you need a pre-sized array. The Array() call without new works the same as with it.
Array.of() always treats its arguments as elements, which removes the single-number ambiguity.
Array.from() builds an array from any iterable or array-like object, and takes an optional mapping function.
The { length: 5 } form is the standard way to generate a sequence or a filled array with real elements, not holes. Use it for ranges and for building a grid, for example Array.from({ length: 3 }, () => Array(3).fill(0)).
The spread operator creates a new array from the elements of another.
Spread is a shallow copy. Objects inside the array are shared between the copy and the original. For a deep copy use structuredClone(original), available in modern browsers and Node.js.
Other ways to create arrays from existing data:
"a,b,c".split(",") returns ["a", "b", "c"].Object.keys(obj), Object.values(obj) and Object.entries(obj) return arrays of keys, values and pairs.[...map] or Array.from(map) turns a Map or Set into an array.ES2023 added methods that return a new array instead of changing the original, which fits immutable patterns used in React and similar libraries.
Compare this with sort(), reverse() and splice(), which change the array in place. These newer methods are supported in current evergreen browsers and recent Node.js versions, but check your support targets before using them in older environments.
| Need | Use |
|---|---|
| Known values | [1, 2, 3] |
| Single numeric element | Array.of(5) or [5] |
| Fixed length filled with a value | Array(n).fill(value) |
| Sequence or computed values | Array.from({ length: n }, fn) |
Convert a string, Set, Map or NodeList | Array.from(x) or [...x] |
| Shallow copy | [...arr] |
| Deep copy | structuredClone(arr) |
Use Array.isArray(value) to check whether something is an array. typeof [] returns "object", so it cannot tell arrays from other objects.
new Array(3) creates an empty array of length 3, while [3] creates an array with the single element 3. Array.of(3) matches the literal.
Use new Array(5).fill(0) or Array.from({ length: 5 }, () => 0). Do not use fill with an object or inner array, because every slot would share the same reference.
Use Array.from({ length: rows }, () => Array(cols).fill(0)). Each row is then a separate array.
They are objects with special length behaviour, though engines optimise dense arrays of the same element type into contiguous storage.
Use an array literal for most cases, Array.from for iterables and generated sequences, Array.of to avoid the single-number trap and the spread operator for shallow copies. Avoid new Array(n) unless you immediately fill it, and use the non-mutating methods toSorted, toReversed and with when you need changed copies.