← Back to Spider On The Web

JavaScript Array Methods

The essential methods you'll use every day

Transform Arrays

.map() Transform

Transform each element and return a new array. Original array unchanged.

const nums = [1, 2, 3];
const doubled = nums.map(x => x * 2);
// Result: [2, 4, 6]
.filter() Transform

Keep only elements that pass a test. Returns a new array.

const nums = [1, 2, 3, 4, 5];
const big = nums.filter(x => x > 2);
// Result: [3, 4, 5]
.reduce() Transform

Reduce array to a single value. Sum, flatten, group — very powerful.

const nums = [1, 2, 3, 4];
const sum = nums.reduce((acc, x) => acc + x, 0);
// Result: 10

Search Arrays

.find() Search

Return the first element that passes a test. Returns undefined if none found.

const users = [{name: 'Ana'}, {name: 'Bob'}];
const bob = users.find(u => u.name === 'Bob');
// Result: {name: 'Bob'}
.findIndex() Search

Return the index of the first matching element. Returns -1 if not found.

const nums = [10, 20, 30];
const idx = nums.findIndex(x => x > 15);
// Result: 1
.indexOf() Search

Return index of a specific value. Simple equality check, no callback.

const colors = ['red', 'green', 'blue'];
const idx = colors.indexOf('green');
// Result: 1

Check Arrays

.some() Check

Check if ANY element passes a test. Returns true/false.

const nums = [1, 2, 3];
const hasEven = nums.some(x => x % 2 === 0);
// Result: true
.every() Check

Check if ALL elements pass a test. Returns true/false.

const nums = [2, 4, 6];
const allEven = nums.every(x => x % 2 === 0);
// Result: true
.includes() Check

Check if array contains a value. Simple and clean.

const fruits = ['apple', 'banana', 'mango'];
const hasBanana = fruits.includes('banana');
// Result: true

Loop Through Arrays

.forEach() Loop

Execute a function on each element. Doesn't return anything — just runs code.

const nums = [1, 2, 3];
nums.forEach(x => console.log(x));
// Logs: 1, 2, 3
💡 When to Use What

.map() → Transform each item
.filter() → Remove items
.reduce() → Combine into one value
.find() → Get first match
.some() / .every() → Yes/no questions
.forEach() → Just do something (no return needed)