JavaScript

JavaScript Array Methods Cheatsheet

Quick reference for JavaScript array methods including map, filter, reduce, find, some, every, flat, sort, and more.

Creating Arrays

const a = [1, 2, 3];
const b = Array(5).fill(0);            // [0, 0, 0, 0, 0]
const c = Array.from({ length: 5 }, (_, i) => i); // [0, 1, 2, 3, 4]
const d = Array.from("hello");         // ['h', 'e', 'l', 'l', 'o']
const e = Array.of(1, 2, 3);           // [1, 2, 3]

Iterating

MethodReturnsMutates
forEach(fn)undefinedNo
map(fn)New arrayNo
filter(fn)New arrayNo
reduce(fn, init)Single valueNo
reduceRight(fn, init)Single valueNo
flatMap(fn)New arrayNo
[1, 2, 3].map(x => x * 2);              // [2, 4, 6]
[1, 2, 3, 4].filter(x => x > 2);        // [3, 4]
[1, 2, 3].reduce((sum, x) => sum + x, 0); // 6
[[1, 2], [3]].flatMap(x => x);           // [1, 2, 3]

Searching

MethodReturnsDescription
find(fn)Element or undefinedFirst match
findIndex(fn)Index or -1Index of first match
findLast(fn)Element or undefinedLast match
findLastIndex(fn)Index or -1Index of last match
indexOf(val)Index or -1First index of value
lastIndexOf(val)Index or -1Last index of value
includes(val)booleanContains value
[1, 2, 3].find(x => x > 1);           // 2
[1, 2, 3].findIndex(x => x > 1);      // 1
[1, 2, 3].includes(2);                 // true

Testing

MethodReturnsDescription
some(fn)booleanAt least one matches
every(fn)booleanAll match
[1, 2, 3].some(x => x > 2);           // true
[1, 2, 3].every(x => x > 0);          // true

Adding & Removing

MethodReturnsMutatesDescription
push(...items)New lengthYesAdd to end
pop()Removed itemYesRemove from end
unshift(...items)New lengthYesAdd to start
shift()Removed itemYesRemove from start
splice(i, n, ...items)Removed itemsYesRemove/insert at index
concat(...arrays)New arrayNoMerge arrays
toSpliced(i, n, ...items)New arrayNoImmutable splice
const arr = [1, 2, 3];
arr.splice(1, 1, 10, 20);             // arr is [1, 10, 20, 3]
[1, 2].concat([3, 4]);                // [1, 2, 3, 4]

Sorting & Reversing

MethodReturnsMutates
sort(fn?)Sorted arrayYes
reverse()Reversed arrayYes
toSorted(fn?)New sorted arrayNo
toReversed()New reversed arrayNo
[3, 1, 2].sort((a, b) => a - b);      // [1, 2, 3] (ascending)
[3, 1, 2].sort((a, b) => b - a);      // [3, 2, 1] (descending)
["b", "a"].sort();                     // ["a", "b"] (lexicographic)

Flattening

[1, [2, [3]]].flat();                  // [1, 2, [3]] (depth 1)
[1, [2, [3]]].flat(Infinity);          // [1, 2, 3]

Slicing & Joining

MethodReturnsMutatesDescription
slice(start, end)New arrayNoExtract section
join(sep)StringNoJoin elements
at(index)ElementNoElement at index (supports negative)
with(index, val)New arrayNoImmutable set at index
[1, 2, 3, 4].slice(1, 3);             // [2, 3]
[1, 2, 3].join("-");                   // "1-2-3"
[1, 2, 3].at(-1);                     // 3
[1, 2, 3].with(1, 99);                // [1, 99, 3]

Spread & Destructuring

const copy = [...arr];                 // Shallow copy
const merged = [...a, ...b];           // Merge arrays
const [first, ...rest] = arr;          // Destructure
const unique = [...new Set(arr)];      // Remove duplicates

Static Methods

MethodDescription
Array.isArray(val)Check if value is an array
Array.from(iterable, fn?)Create array from iterable
Array.of(...items)Create array from arguments

Common Recipes

// Sum
arr.reduce((sum, x) => sum + x, 0);

// Max / Min
Math.max(...arr);
Math.min(...arr);

// Group by (Object.groupBy)
Object.groupBy(users, u => u.role);

// Chunk array
function chunk(arr, size) {
  return Array.from({ length: Math.ceil(arr.length / size) },
    (_, i) => arr.slice(i * size, i * size + size));
}

// Remove duplicates
[...new Set(arr)];

// Count occurrences
arr.reduce((acc, val) => ({ ...acc, [val]: (acc[val] || 0) + 1 }), {});