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
.map() to render 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).
.find() when you need one specific item — looking up a user by ID, finding a product by slug, getting the current route.
.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.
.includes() — check if item exists
.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.
- Accumulator (
sum) — the running result - Current item (
price) — the current array element
.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:Common mistakes
Forgetting to return in .map()
Forgetting to return in .map()
Using .map() when you need .forEach()
Using .map() when you need .forEach()
Mutating the original array by mistake
Mutating the original array by mistake
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