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.

🛒
UrbanCart Example
Log each product in the catalog to the admin console

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.js Iterate & Log
Output
1. Wireless Mouse — ₹1499 2. Mechanical Keyboard — ₹3999 3. USB-C Hub — ₹2499 4. 4K Monitor — ₹28999 5. Laptop Stand — ₹1899 Return value: undefined
forEach vs. for loop: 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.

map.js Transform Data
Output
["Wireless Mouse | ₹1,499", "Mechanical Keyboard | ₹3,999", "4K Monitor | ₹28,999", "Laptop Stand | ₹1,899"] [ {"name":"Wireless Mouse","basePrice":1499,"gst":270,"total":1769}, {"name":"Mechanical Keyboard","basePrice":3999,"gst":720,"total":4719}, {"name":"4K Monitor","basePrice":28999,"gst":5220,"total":34219}, {"name":"Laptop Stand","basePrice":1899,"gst":342,"total":2241} ]
map always returns a new array with the same length as the original. If you don't need the returned array, use 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).

filter.js Filter Products
Output
Accessories: 4 Under ₹3000: ["Wireless Mouse", "USB-C Hub", "Laptop Stand"] In stock: ["Wireless Mouse", "Mechanical Keyboard", "4K Monitor", "Webcam HD"]
filter never mutates. The original array stays intact. The returned array may be shorter than the original — or even empty if nothing passes the test. This is perfect for search, category pages, and inventory filtering in UrbanCart.

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.

💰
UrbanCart Example
Calculate the shopping cart total at checkout

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.

reduce.js Cart Total
Output
Cart total: ₹8594 Total items: 6 Wireless Mouse x1 = ₹1499 Mechanical Keyboard x1 = ₹3999 USB-C Cable x3 = ₹1197 Laptop Stand x1 = ₹1899
The second argument matters! Always pass an initial value (the 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.js Find Product
Output
Found: {"id":103,"name":"USB-C Hub","price":2499} Keyboard price: ₹3999 Monitor is at index: 3 Missing: undefined
find vs. filter: 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-every.js Boolean Checks
Output
Any item over ₹5000? true Any item over ₹50000? false All in stock? false All under ₹50000? true
Practical use: In UrbanCart, 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.

sort.js Sort Products
Output
Cheapest first: Wireless Mouse — ₹1499 Laptop Stand — ₹1899 Mechanical Keyboard — ₹3999 Webcam HD — ₹4599 4K Monitor — ₹28999 Most expensive first: 4K Monitor — ₹28999 Webcam HD — ₹4599 Mechanical Keyboard — ₹3999 Laptop Stand — ₹1899 Wireless Mouse — ₹1499 Sorted prices: [5, 30, 50, 200, 1000]
Gotcha — sort mutates! Unlike 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.

⛓️
UrbanCart Example
Build a filtered, sorted product display pipeline

On the UrbanCart category page, products are filtered by availability, transformed into display cards, and sorted by price — all in a single chain.

chaining.js Method Pipeline
Output
In-stock accessories (cheapest first): Wireless Mouse — ₹1499 Mechanical Keyboard — ₹3999 Webcam HD — ₹4599 Total in-stock value: ₹38996
Read the chain left to right: 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 — Filter, Sort & Reduce
Output

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.

Try It Yourself — Method Chaining Pipeline
Output

Test your knowledge

Question 1 of 5
What does map() return?
Question 2 of 5
Which method would you use to get only in-stock products from an array?
Question 3 of 5
What is the second argument to reduce(callback, ???) used for?
Question 4 of 5
Which array method mutates the original array?
Question 5 of 5
What does products.some(p => p.price > 5000) return if at least one product costs over ₹5000?
0
/5
Great job!

Challenge: UrbanCart Order Report

Build a complete order report pipeline using the methods you just learned:

  1. Start with an array of 8 products (name, price, qty, inStock)
  2. Use filter to keep only in-stock items
  3. Use sort to order by price (descending)
  4. Use map to create display strings: "ProductName — ₹price x qty = ₹subtotal"
  5. Use reduce to calculate the grand total
  6. Use some to check if any item costs more than ₹10,000
  7. Use every to verify all items have qty > 0
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →