RSRobince Studio

Array Methods

Modern arrays come with powerful methods that let you transform data without writing manual loops.

  • map builds a new array by transforming each item
  • filter keeps only items that pass a test
  • reduce combines all items into one value
  • forEach runs 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…