I write visual, interactive essays about code, creative coding, and software engineering. Each post aims to explain concepts from the ground up with hands-on demos.

← Back

JavaScript Array Methods Cheatsheet

2 min readweb

A quick reference for JavaScript array methods. Try the interactive demo below!

Interactive Demo

Click on different methods to see how they transform arrays:

Input:
1
2
3
[1, 2, 3].map(x => x * 2)
Output:
2
4
6

Transforming Arrays

map()

Transform each element:

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

filter()

Keep elements that pass a test:

const evens = [1, 2, 3, 4].filter(x => x % 2 === 0);
// [2, 4]

reduce()

Reduce to a single value:

const sum = [1, 2, 3].reduce((acc, x) => acc + x, 0);
// 6

Finding Elements

find()

Find first matching element:

const found = [1, 2, 3].find(x => x > 1);
// 2

includes()

Check if element exists:

[1, 2, 3].includes(2); // true

Modifying Arrays

push() / pop()

Add/remove from end:

const arr = [1, 2];
arr.push(3);  // [1, 2, 3]
arr.pop();    // [1, 2]

slice()

Extract portion (doesn't modify original):

[1, 2, 3, 4].slice(1, 3); // [2, 3]

splice()

Remove/replace elements (modifies original):

const arr = [1, 2, 3];
arr.splice(1, 1); // removes index 1
// arr is now [1, 3]

Bookmark this for quick reference!