Free beginner tutorial

JavaScript Arrays and Array Methods

Create and update JavaScript arrays, then use map, filter, find, some, every, reduce and sort to work with the values in a list.

Create an array

An array stores an ordered list of values. Each item has a zero-based index, so the first item is at index 0.

const colors = ["blue", "green", "orange"];

console.log(colors[0]); // "blue"
console.log(colors.length); // 3

Add or update items

Use push() to add an item to the end. Use an index when you need to replace an existing item.

colors.push("purple");
colors[1] = "teal";

Loop through a list

A for...of loop reads each value in order. It works well when you want to perform the same action for every item.

for (const color of colors) {
  console.log(color);
}

Use common array methods

includes() checks whether an array contains a value. map() builds a new array by transforming each item, and filter() builds a new array from items that pass a condition.

const scores = [58, 82, 91];

const hasHighScore = scores.includes(91);
const doubled = scores.map(score => score * 2);
const passing = scores.filter(score => score >= 70);

map() and filter() leave the original array unchanged. By contrast, push() changes the array it is called on.

Find a value or check a condition

Use find() to get the first item that matches a test. It returns undefined when there is no match. Use some() to check whether at least one item matches, or every() to check whether all items match.

const firstPassingScore = scores.find(score => score >= 70);
const hasLowScore = scores.some(score => score < 60);
const allAreNumbers = scores.every(score => typeof score === "number");

console.log(firstPassingScore); // 82
console.log(hasLowScore);       // true
console.log(allAreNumbers);     // true

These methods answer different questions: find() returns an item, while some() and every() return Boolean values. They stop checking once the answer is known.

Reduce an array to one value

reduce() passes an accumulated result from one item to the next. It can calculate a total, and providing an initial value makes the starting result clear, including when the array is empty.

const totalScore = scores.reduce(
  (total, score) => total + score,
  0
);

console.log(totalScore); // 231

For a simple total, a regular loop may be easier to read. Choose reduce() when carrying a result from one step to the next makes the operation clear.

Array questions

Why does the first array item use index zero?

JavaScript array indexes count from zero, so for an array with three items, the valid indexes are 0, 1, and 2. The item count is available from the length property.

How do I add an item to the end?

Call push(value). It updates the existing array and returns its new length.

What is the difference between map() and filter()?

map() transforms every item and returns a new array of results. filter() returns a new array containing only the items that pass its condition.

How do I find the first matching item?

Use find() with a condition. It returns the first matching item, or undefined when no item matches.

Does reduce() change the original array?

reduce() does not change the array by itself. Its callback can still modify objects or other values it receives, so avoid changing shared data unless that is intended.

Sort numbers without changing the original list

By default, sort() compares values as strings, so numeric values can appear in an unexpected order. It also changes the array it is called on. Provide a numeric comparison function and copy the array first when you want to keep its original order:

const scoresToSort = [8, 20, 3];
const ascendingScores = [...scoresToSort].sort((a, b) => a - b);

console.log(ascendingScores); // [3, 8, 20]
console.log(scoresToSort);    // [8, 20, 3]

Read MDN's references for map(), filter(), find(), some(), every(), reduce() and sort() for syntax, examples and edge cases.

Use arrays for ordered collections. For named properties that describe one thing, use an object.
Practice: Change the scores and comparison function in the HTML, CSS and JavaScript editor.