Array Methods
Modern arrays come with powerful methods that let you transform data without writing manual loops.
mapbuilds a new array by transforming each itemfilterkeeps only items that pass a testreducecombines all items into one valueforEachruns a function for each item
const nums = [1, 2, 3, 4];
const doubled = nums.map(n => n * 2);
const evens = nums.filter(n => n % 2 === 0);
const total = nums.reduce((sum, n) => sum + n, 0);
map, filter and reduce return new values and leave the original array unchanged.
Try it yourself
Loading editor…