Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array — JavaScript arrays

Array is the built-in constructor for arrays: ordered, zero-indexed collections of values. Learn array creation, the length property, and the difference between an empty slot and an element whose value is undefined.

Version: ECMAScript built-in: the Array constructor dates to the first edition (1997). Array literals were introduced in ES3 (1999), Array.isArray in ES5 (2009), and Array.of in ES2015. Verification in Node.js 24 does not establish browser support.

Syntax

[element1, element2]
new Array(length)
Array.of(element1, element2)
Array.isArray(value)

Array is the built-in array constructor in ECMAScript, not a Web API. An array is an object with special handling for indices and the length property. It can contain values of different types. The first element has index 0; an array literal such as [10, 20] is usually the clearest way to create one.

Watch out for a numeric argument: new Array(3) creates an array with length equal to 3, but no elements at those indices. Array.of(3) and [3] create one element whose value is 3. An empty slot is not the same as an existing element whose value is undefined; 0 in sparse in the example produces false.

length does not necessarily count existing elements. Writing at a higher index can increase it and leave empty slots. In an ordinary, mutable array, reducing length deletes elements at indices greater than or equal to the new length. delete values[0] removes an element without shortening the array.

push() mutates the array. By default, map() returns a new array of results, but a custom constructor selected via Symbol.species can make it return an existing object instead. For each index it considers, map() checks whether the corresponding property exists on the array itself or in its prototype chain. If the array inherits that property, map() calls the callback for that index even though it has no own element there. Array copies are shallow: nested objects can remain shared. Use Array.isArray(value) to identify arrays, because typeof returns "object" for them.

Examples

const values = [10, 20];
values.push(30);
const doubled = values.map(value => value * 2);
const sparse = new Array(3);
const single = Array.of(3);
console.log(JSON.stringify(values));
console.log(JSON.stringify(doubled));
console.log(`${sparse.length},${0 in sparse}`);
console.log(JSON.stringify(single));
console.log(Array.isArray(values));

Result

[10,20,30]
[20,40,60]
3,false
[3]
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources