forEach
forEach() calls a function once for each element in an array. It's the simplest way to iterate without creating a new array — use it when you want side effects like logging or updating the DOM.
When an UrbanCart admin opens the product dashboard, each product is logged to the console for debugging. forEach iterates through the catalog without transforming it.
forEach is cleaner than a traditional for loop, but you cannot break out of it. If you need early exit, use a for...of loop instead. For transforming data, use map.
map
map() creates a new array by calling a function on every element. The original array stays unchanged. Use it whenever you need to transform data.
forEach instead. Using map for side effects is an anti-pattern.
filter
filter() creates a new array containing only the elements that pass a test. The callback must return true (keep) or false (skip).
reduce
reduce() boils an entire array down to a single value — a number, a string, an object. It's the most powerful array method. The classic use case: calculating a cart total.
When a customer clicks "Proceed to Checkout," UrbanCart uses reduce to sum up every item's price multiplied by its quantity into a single total.
0 after the callback). Without it, reduce uses the first element as the initial value — which causes bugs when the array is empty or contains objects.
find and findIndex
find() returns the first element that matches a test. findIndex() returns its position. Both stop searching as soon as they find a match.
find returns a single element (or undefined). filter returns an array of all matches. Use find when you know there's only one match — like looking up a product by ID.
some and every
some() checks if at least one element passes a test. every() checks if all elements pass. Both return a boolean.
some checks if any cart item is out of stock before checkout. every validates that all required form fields are filled. Both short-circuit — they stop iterating as soon as the answer is known.
sort
sort() arranges elements in place. By default it converts values to strings and sorts alphabetically — which breaks numbers. Always pass a compare function for numbers and objects.
filter and map, sort() changes the original array. To sort without mutating, spread into a new array first: [...products].sort(...). This is a very common interview question.
Chaining methods
Because filter, map, and sort all return arrays, you can chain them into a pipeline. This is the most powerful pattern in JavaScript data handling.
On the UrbanCart category page, products are filtered by availability, transformed into display cards, and sorted by price — all in a single chain.
filter narrows the data, sort orders it, map transforms it. Each step returns a new array that feeds into the next. This pattern is used in every real-world React, Node.js, and API project.
Try It Yourself #1 — Filter, sort, reduce
Filter UrbanCart products by category, sort by price, and calculate the cart total using reduce. Edit the code and click Run.
Try It Yourself #2 — Chain methods
Build a full pipeline: filter in-stock products, map to display cards, and sort by price. Edit the code and click Run.
Test your knowledge
map() return?reduce(callback, ???) used for?products.some(p => p.price > 5000) return if at least one product costs over ₹5000?Challenge: UrbanCart Order Report
Build a complete order report pipeline using the methods you just learned:
- Start with an array of 8 products (name, price, qty, inStock)
- Use
filterto keep only in-stock items - Use
sortto order by price (descending) - Use
mapto create display strings:"ProductName — ₹price x qty = ₹subtotal" - Use
reduceto calculate the grand total - Use
someto check if any item costs more than ₹10,000 - Use
everyto verify all items have qty > 0
You just mastered JavaScript array methods.
Now build a full-stack career.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →