Skip to main content

The methods you’ll use every day

JavaScript arrays have dozens of methods, but you’ll use about six of them 90% of the time. These are the ones that matter for web development and React. Every method here takes a callback function — an arrow function that runs once for each item in the array.

.map() — transform each item

.map() creates a new array by transforming every item. Use it when you need to convert data from one shape to another.

Real-world: formatting API data

In React, you’ll use .map() to render lists:
.map() always returns a new array with the same number of items. It never modifies the original. This is exactly what React needs for rendering lists.

.filter() — keep matching items

.filter() creates a new array with only the items that pass a test. The callback must return true or false.

Real-world: filtering a user list

.filter() can return fewer items, the same number, or even an empty array. It never modifies the original.

.find() — get the first match

.find() returns the first item that matches. Unlike .filter(), it returns a single item (or undefined if nothing matches).
Use .find() when you need one specific item — looking up a user by ID, finding a product by slug, getting the current route.
.find() returns undefined if no item matches — not null, not an error. Always handle the “not found” case.

.forEach() — do something with each item

.forEach() runs a function for each item but doesn’t return anything. Use it for side effects like logging or DOM manipulation.
If you need a new array, use .map(). If you need to do something with each item (log, send, update DOM), use .forEach(). The rule: .map() transforms, .forEach() performs actions.

.includes() — check if item exists

Simple and readable. Works great for checking roles, features, or any “is this in the list?” question.

.reduce() — combine into a single value

.reduce() takes an array and reduces it down to a single value — a total, an object, a string, whatever you need.
The callback takes two arguments:
  1. Accumulator (sum) — the running result
  2. Current item (price) — the current array element
The second argument to .reduce() (0) is the initial value of the accumulator.

More reduce examples

.reduce() is powerful but can be hard to read. If a .map() + .filter() combination does the job, prefer that over .reduce(). Readability matters more than cleverness.

Chaining methods

Array methods return arrays, so you can chain them:
Read chains top to bottom: start with all products → keep only in-stock → keep only under $500 → extract names.

Common mistakes

This is the same arrow function gotcha from earlier. With curly braces, you need return. Without curly braces, the return is implicit. This mistake causes arrays of undefined.
.map() creates and returns a new array. If you don’t need that array, use .forEach() instead. Using .map() for side effects is wasteful and signals to other developers that you intended to transform data.
.sort(), .reverse(), .splice(), .push(), and .pop() all mutate the original array. If you need the original intact, spread it into a new array first: [...arr].sort().

What’s next?

Arrays handle lists. Now let’s learn about objects — JavaScript’s key-value data structure for representing entities like users, products, and API responses.

Objects

Store data as key-value pairs