Stop writing for loops. Modern JS relies on functional programming methods: Map, Filter, and Reduce.
The Pipeline (Chaining)
Clean Code Power Move
You can attach methods together like Lego bricks. The output of one becomes the input of the next.
javascript
const products = [
{ name: "Laptop", price: 1000, inStock: true },
{ name: "Phone", price: 500, inStock: false },
{ name: "Tablet", price: 300, inStock: true }
];
// Goal: Get total value of available items
const totalValue = products
.filter(item => item.inStock) // 1. Remove out of stock
.map(item => item.price) // 2. Extract just prices
.reduce((acc, price) => acc + price, 0); // 3. Sum them up
console.log(totalValue); // 1300 (1000 + 300)Map vs ForEach
Use map when you want to create a new array. Use forEach when you just want to do something (like log to console) and return nothing.