JavaScript Array Methods

Quick reference for map, filter, reduce, and more — with copy-paste examples

Transformation (Create New Array)
Transform .map() — transform each element

Creates a new array by calling a function on every element in the original array. Does not mutate the original.

const doubled = numbers.map(n => n * 2);
const names = users.map(u => u.name);
💡 Tip: Use map() when you need to transform data. Avoid mutating the original array — it returns a new one.
Transform .filter() — select elements by condition

Creates a new array with all elements that pass the test implemented by the provided function.

const evens = numbers.filter(n => n % 2 === 0);
const active = users.filter(u => u.active);
💡 Tip: filter() returns a subset. Original array is unchanged.
Aggregation (Single Value)
Aggregate .reduce() — accumulate to single value

Executes a reducer function on each element, resulting in a single output value. Extremely flexible.

const sum = numbers.reduce((acc, n) => acc + n, 0);
const max = numbers.reduce((a, b) => Math.max(a, b));
💡 Tip: Second argument is the initial accumulator value. Omit it to use first array element as initial.
Aggregate .reduceRight() — reduce from right to left

Works like reduce() but starts from the last element and moves backward.

const flattened = [].concat(...arrays.reduceRight((a, b) => [b, ...a]));
💡 Use case: Flattening nested arrays or composing functions right-to-left.
Search & Find
Search .find() — first matching element

Returns the first element that satisfies the provided testing function. Returns undefined if none found.

const user = users.find(u => u.id === 123);
💡 Tip: Use find() when you expect at most one match. Use filter() for multiple matches.
Search .findIndex() — index of first match

Returns the index of the first element that satisfies the testing function. Returns -1 if not found.

const idx = users.findIndex(u => u.email === 'test@example.com');
Search .includes() — check if value exists

Determines whether an array includes a certain value among its entries. Simple and readable.

const hasAdmin = roles.includes('admin');
💡 Remember: Uses === for comparison. For objects, checks reference equality, not content.
Search .indexOf() — first index of value

Returns the first index at which a given element can be found. Returns -1 if not present.

const idx = items.indexOf('apple');
⚠️ indexOf() uses === and does not work with NaN. Use findIndex() for complex checks.
Search .lastIndexOf() — last index of value

Returns the last index at which a given element can be found. Searches backwards from the end.

const last = data.lastIndexOf('error');
Boolean Tests
Boolean .some() — at least one passes

Tests whether at least one element passes the test. Short-circuits on first match (efficient).

const hasAdult = users.some(u => u.age >= 18);
💡 Short-circuit: Stops iterating once a match is found. Faster than filter().length > 0.
Boolean .every() — all pass

Tests whether all elements pass the test. Short-circuits on first failure.

const allActive = users.every(u => u.active);
💡 Empty arrays: every() returns true for empty arrays (vacuous truth).
Array Manipulation

Mutation Methods (modify original array)

.push() — add to end
arr.push(x) ← returns new length
.pop() — remove from end
arr.pop() ← returns removed element
.unshift() — add to start
arr.unshift(x) ← returns new length
.shift() — remove from start
arr.shift() ← returns removed element
⚠️ These mutate the original array. Prefer slice(), concat(), or spread for immutable patterns.
Manipulate .slice() — extract portion (non-mutating)

Returns a shallow copy of a portion of an array. Does not modify the original.

const first3 = arr.slice(0, 3); // [0, 3)
const tail = arr.slice(1); // from index 1 to end
const copy = arr.slice(); // clone entire array
💡 Shallow copy: Nested objects are still shared between original and slice.
Manipulate .splice() — change array in-place

Changes array contents by removing/replacing existing elements and/or adding new ones. Mutates original. Returns removed elements.

// Remove 1 element at index 2
const removed = arr.splice(2, 1);
// Insert without removing
arr.splice(2, 0, 'new');
// Replace 1 element at index 1
arr.splice(1, 1, 'replacement');
⚠️ Mutates original: splice() alters the source array. Use slice() for non-destructive.
Manipulate .concat() — merge arrays

Merges two or more arrays. Returns a new array. Does not mutate originals.

const merged = arr1.concat(arr2, arr3);
const withExtra = arr.concat('a', 'b');
💡 For single-level merging, spread is cleaner: [...arr1, ...arr2]. concat() also flattens one level automatically.
Manipulate .join() — array to string

Joins all elements into a string, separated by the specified separator. Default separator is comma.

const csv = arr.join(',');
const sentence = words.join(' ');
const path = parts.join('/');
Manipulate .split() — string to array (String method)

Splits a string into an array of substrings using the specified separator. String method, not Array method.

const parts = 'a,b,c'.split(',');
const words = sentence.split(' ');
💡 split() is on String.prototype, not Array. Remember: arrays use join(), strings use split().
Manipulate .reverse() — reverse in-place

Reverses the array in place. Mutates original and returns it.

const reversed = arr.slice().reverse(); // safe: copy first
⚠️ reverse() mutates. To avoid side effects: [...arr].reverse() or arr.slice().reverse().
Manipulate .sort() — sort in-place

Sorts array elements in place. Default sort is lexicographic (string) order. Always provide a compare function for numbers.

// Numeric ascending
arr.sort((a, b) => a - b);
// Descending
arr.sort((a, b) => b - a);
// By property
users.sort((a, b) => a.name.localeCompare(b.name));
⚠️ sort() mutates. For immutable: [...arr].sort(compare). Default sort breaks numbers: [10,2,1].sort() → [1,10,2].
Iteration
Iterate .forEach() — execute for each element

Executes a provided function once for each array element. No return value (undefined). Cannot be chained.

users.forEach(u => console.log(u.name));
arr.forEach((val, idx, array) => { /* ... */ });
⚠️ Use forEach() for side effects only. For transformations, use map(). Cannot break early — use for...of or some() if you need to exit early.

Iterators (for...of loops)

.entries()
for (const [i, v] of arr.entries()) { }
.keys()
for (const i of arr.keys()) { }
.values()
for (const v of arr.values()) { }
💡 These return iterators. Use with for...of or Array.from(). entries() gives [index, value] pairs.
Info & Checks

.length

arr.length — number of elements
Not a method; a property. Updating length truncates or extends array.
Info Array.isArray() — check if value is array

Determines whether the passed value is an array. Safer than instanceof Array (works across frames).

if (Array.isArray(value)) { /* safe */ }
⚠️ typeof [] === 'object' — always use Array.isArray() to reliably detect arrays.
Manipulate .flat() / .flatMap() — flatten arrays

flat() creates a new array with all sub-array elements concatenated recursively. flatMap() maps then flattens one level.

const flat = nested.flat(); // depth=1 default
const deep = nested.flat(2); // depth 2
const result = arr.flatMap(x => [x, x * 2]);
💡 flatMap(flatMap) is faster than map().flat(). Great for 1-to-many transformations.

About This Tool

The JavaScript Array Methods cheat sheet provides quick, searchable reference for the most common array methods. Each method includes syntax, practical examples, copy-paste code, and usage tips. Whether you're deciding between map() vs forEach(), or need a quick reduce() example, this guide has you covered.

When to Use Each Method

🔄 Transformation

map() transforms each element. filter() selects a subset. Both return new arrays.

🧮 Aggregation

reduce() rolls up array into single value: sum, product, object, or anything custom.

🔍 Search

find() returns first match. includes() checks existence. some()/every() for boolean tests.

✂️ Manipulate

slice() non-destructive. splice() mutates. concat() merges. sort()/reverse() mutate.

Frequently Asked Questions

What's the difference between map() and forEach()?

map() returns a new array with transformed values and is chainable. forEach() returns undefined and is meant for side effects only (logging, mutating external vars). Use map() for data transformation, forEach() for iteration with side effects.

Why does sort() break my numbers?

Default sort() converts elements to strings and compares lexicographically, so [10, 2, 1].sort() gives [1, 10, 2]. Always provide a compare function: arr.sort((a,b) => a - b) for numeric ascending.

When should I use reduce()?

Use reduce() when you need to combine array elements into a single value — sum, max, min, product, frequency map, grouping, or flattening. It's the most powerful but also most complex array method. If a simpler method (map, filter, find) fits, use that instead for readability.

Is this tool free?

Yes, completely free with no sign-up required.

Related Tools