Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to JavaScript
JavaScript Array Methods You Must Know

JavaScript Array Methods You Must Know

JavaScript2,625 viewsBy Admin
javascriptarraymethodsmust

The Essential Array Methods

Modern JavaScript gives you powerful built-in array methods that replace manual for loops. Here are the ones you will use every day, with real examples.

map() — Transform Every Element

const prices = [10, 20, 30];
const withTax = prices.map(p => p * 1.1);
// [11, 22, 33]

filter() — Keep Matching Elements

const nums = [1, 2, 3, 4, 5, 6];
const evens = nums.filter(n => n % 2 === 0);
// [2, 4, 6]

reduce() — Collapse to a Single Value

const cart = [{ price: 30 }, { price: 50 }, { price: 20 }];
const total = cart.reduce((sum, item) => sum + item.price, 0);
// 100

find() & findIndex()

const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
users.find(u => u.id === 2);      // { id: 2 }
users.findIndex(u => u.id === 2); // 1

some() & every()

[1, 2, 3].some(n => n > 2);   // true  (at least one)
[1, 2, 3].every(n => n > 0);  // true  (all of them)

Quick Reference Table

MethodReturnsUse for
mapNew arrayTransforming data
filterNew arrayRemoving items
reduceSingle valueTotals, grouping
findOne elementLookup
forEachundefinedSide effects only

Chaining Them Together

const result = products
  .filter(p => p.inStock)
  .map(p => p.price)
  .reduce((a, b) => a + b, 0);

FAQs

map vs forEach — what's the difference?

map returns a new array; forEach returns nothing and is used purely for side effects.

Do these methods mutate the original array?

No — map, filter, and reduce all return new arrays. sort, splice, and reverse do mutate. See more JavaScript guides.