Skip to content

Arrays with JavaScript

An array is an ordered list of values held in one variable. Each value sits at a numbered position, called its index, and the first index is 0. Arrays are the usual way to hold a collection that a program loops over, such as the items in a basket, the rows of a table or the results of a search.

An array literal lists the values between square brackets. length gives the number of items, and an index in square brackets reads one item:

const basket = ['apples', 'bread', 'milk'];
console.log(basket);
console.log(basket.length);
console.log(basket[0]);
console.log(basket[basket.length - 1]);
console.log(basket.at(-1));
console.log(basket[5]);

This prints:

[ 'apples', 'bread', 'milk' ]
3
apples
milk
milk
undefined

The last item is at length - 1, because counting starts at 0. The at() method also accepts a negative index, which counts back from the end, so at(-1) is the last item. Reading an index that has no item gives undefined rather than an error.

push() adds one or more items to the end and returns the new length. pop() removes the last item and returns it:

const basket = ['apples', 'bread'];
const newLength = basket.push('milk', 'eggs');
console.log(newLength, basket);
const removed = basket.pop();
console.log(removed, basket);

This prints:

4 [ 'apples', 'bread', 'milk', 'eggs' ]
eggs [ 'apples', 'bread', 'milk' ]

Used together, push() and pop() make the array behave as a stack, where the last item in is the first out.

unshift() adds items to the start, and shift() removes the first item and returns it. Every other item moves up or down one index to make room:

const queue = ['Ana', 'Ben'];
queue.unshift('Chloe');
console.log(queue);
const first = queue.shift();
console.log(first, queue);
const empty = [];
console.log(empty.pop(), empty.shift());

This prints:

[ 'Chloe', 'Ana', 'Ben' ]
Chloe [ 'Ana', 'Ben' ]
undefined undefined

push() with shift() makes a queue, where the first item in is the first out. Calling pop() or shift() on an empty array returns undefined and does not throw.

Changing items, and what an array can hold

Section titled “Changing items, and what an array can hold”

Assigning to an index replaces the item there. An array declared with const can still be changed in this way, because const stops the variable being pointed at a different array, not the array’s contents being changed:

const basket = ['apples'];
basket[0] = 'pears';
basket.push('cheese');
console.log(basket);
const record = ['Ana', 34, true, { city: 'Leeds' }];
console.log(record.length, typeof record, Array.isArray(record));

This prints:

[ 'pears', 'cheese' ]
4 object true

One array can hold values of different types, including objects and other arrays. typeof reports an array as object, so use Array.isArray() to test whether a value is an array.

The next page, Manipulating and Sorting Arrays, covers changing items in the middle of an array and putting them in order.

Steve Griffith covers this topic in his video Introduction to Arrays with JavaScript.