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.
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.
| Method | What it returns | Use |
|---|---|---|
map | An array of the same size | Transform every element |
| Array.filter() | A shorter array | Keep only some elements |
| Array.reduce() | A single value | Sum, group, aggregate |
| Array.find() | One element or undefined | Look for the first match |
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)); // 49map 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.
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.
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); // 3Spread (...) 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
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.
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).
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.