Array in JavaScript: creating, looping through and transforming a list

An Array is an ordered list of values indexed from zero, which JavaScript can loop through and transform with dedicated methods.
3 min read
Believemy logo

The array is the most used structure in the language. A list of products, search results, the lines of a shopping cart: as soon as there are several things of the same kind, there is an array.

What makes it distinctive in JavaScript is the number of methods it carries: chosen well, they replace almost every hand-written loop.


Definition

An Array is an ordered list of values reachable by their position. The first one sits at index 0, and the length property gives the number of elements.

An array enforces no type: it can mix numbers, strings and objects. That flexibility is convenient, but an array whose elements do not share the same shape is painful to work with.

JAVASCRIPT
const prices = [19, 49, 99];

console.log(prices.length);   // 3
console.log(prices[0]);       // 19
console.log(prices.at(-1));   // 99, the last one with no index math

prices.push(149);             // appends at the end
console.log(prices);          // [19, 49, 99, 149]


The methods that replace loops

Four methods cover most day-to-day needs. They all take a function as an argument, called on every element.

MethodWhat it returnsUse
mapAn array of the same sizeTransform every element
Array.filter()A shorter arrayKeep only some elements
Array.reduce()A single valueSum, group, aggregate
Array.find()One element or undefinedLook for the first match
JAVASCRIPT
const prices = [19, 49, 99];

console.log(prices.map((p) => p * 2));            // [38, 98, 198]
console.log(prices.filter((p) => p > 20));        // [49, 99]
console.log(prices.reduce((t, p) => t + p, 0));   // 167
console.log(prices.find((p) => p > 20));          // 49

map and Array.filter() never modify the starting array, they return a new one. Array.forEach() and Array.some() round out the family for cases where you only want to walk through the list or answer yes or no.

Good to know

Watch out for the ones that modify in place: sort, reverse, splice and push change the existing array. On shared data, make a copy first.


Copying an array

An array is handled by reference. Assigning one variable to another duplicates nothing, both names point at the same list.

JAVASCRIPT
const original = [1, 2];
const alias = original;
alias.push(3);
console.log(original);         // [1, 2, 3]: same array

const copy = [...original];    // independent copy
copy.push(4);
console.log(original.length);  // 3

Spread (...) is enough for a list of simple values. If the array holds objects, the copy stays shallow: the inner objects are still shared.


Frequently asked questions

Question

How do I check that a value really is an array?

With Array.isArray(value), and only with that. The typeof operator returns "object" for an array just as for any object, so on its own it never tells them apart.


Question

Why does [10, 9, 1].sort() return [1, 10, 9]?

Because sort converts elements to strings before comparing, and "10" comes before "9" alphabetically. For a numeric sort, pass a comparison function: sort((a, b) => a - b). The default behavior is a textbook case of implicit Coercion (type conversion).


Question

Is a for loop still worth using?

Yes, in two cases: when you need to stop halfway through, which map cannot do, and when performance genuinely matters on very large volumes. For everything else for and of stay perfectly readable, but the methods state the intent better. Our JavaScript course makes exactly that shift, from thinking in loops to thinking in transformations, which is how modern libraries are written.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.