JavaScript Array Methods Cheat Sheet
➕ push()
Adds item(s) to the end Returns new length of array Modifies the original array
const nums = [1, 2];
nums.push(3); // [1, 2, 3]
➖ pop()
- Removes last item
- Returns removed value
- Modifies the original array
const nums = [1, 2, 3];
const last = nums.pop(); // 3
⬅️ shift()
- Removes first item
- Returns removed value
- Modifies the original array
const nums = [1, 2, 3];
const first = nums.shift(); // 1
➡️ unshift()
- Adds item(s) to start
- Returns new length
- Modifies the original array
const nums = [2, 3];
nums.unshift(1); // [1, 2, 3]
✂️ splice()
- Add/remove at any index
- Modifies original array
- Returns removed items
const nums = [1, 2, 3, 4];
nums.splice(1, 2); // [2, 3]
🧬 slice()
- Returns a shallow copy
- Doesn’t modify original
- Use for partial clones
const nums = [1, 2, 3, 4];
const copy = nums.slice(1, 3); // [2, 3]
🔗 concat()
- Combines arrays
- Returns a new array
- Original arrays stay unchanged
const a = [1, 2];
const b = [3, 4];
const result = a.concat(b); // [1, 2, 3, 4]
🧵 join()
- Converts array to string
- Takes a separator (like
-
,,
, or''
) - Doesn’t mutate array
const items = ['a', 'b', 'c'];
const str = items.join('-'); // 'a-b-c'
🗺️ map()
- Transforms each item
- Returns a new array
- Original array stays unchanged
const nums = [1, 2, 3];
const doubled = nums.map(n => n * 2); // [2, 4, 6]
🔍 filter()
- Keeps items matching condition
- Returns new filtered array
- Original array stays unchanged
const nums = [1, 2, 3, 4];
const evens = nums.filter(n => n % 2 === 0); // [2, 4]
➕ reduce()
- Reduces array to one value
- Good for sums, counts, etc.
- Original array stays unchanged
const nums = [1, 2, 3, 4];
const sum = nums.reduce((acc, n) => acc + n, 0); // 10
🔎 find()
- Returns first match or undefined
- Doesn’t modify array
- Good for searching one item
const nums = [1, 2, 3];
const found = nums.find(n => n > 1); // 2
🔢 findIndex()
- Returns index of first match
- Returns -1 if not found
- Doesn’t modify array
const nums = [1, 2, 3];
const idx = nums.findIndex(n => n === 2); // 1
✔️ some()
- Checks if any item matches
- Returns boolean
- Doesn’t modify array
const nums = [1, 2, 3];
const hasEven = nums.some(n => n % 2 === 0); // true
✅ every()
- Checks if all items match
- Returns boolean
- Doesn’t modify array
const nums = [2, 4, 6];
const allEven = nums.every(n => n % 2 === 0); // true
🔢 indexOf()
- Returns first index of item
- Returns -1 if not found
- Doesn’t modify array
const fruits = ['apple', 'banana', 'orange'];
const idx = fruits.indexOf('banana'); // 1
🕵️♂️ includes()
- Checks if array contains item
- Returns true or false
- Doesn’t modify array
const fruits = ['apple', 'banana'];
const hasApple = fruits.includes('apple'); // true
🔄 reverse()
- Reverses the order of elements
- Modifies the original array
- Use
.slice().reverse()
to copy
const nums = [1, 2, 3];
nums.reverse(); // [3, 2, 1]
const copy = nums.slice().reverse(); // reversed copy
🔢 sort()
- Sorts array in place
- Modifies original array
- Use compare fn for custom sorting
const nums = [3, 1, 2];
nums.sort(); // [1, 2, 3]
nums.sort((a, b) => b - a); // [3, 2, 1]
🧩 forEach()
- Runs a function on each item
- No returned array
- Good for side effects, logging
const nums = [1, 2, 3];
nums.forEach(n => console.log(n));
📜 Array.from()
- Creates array from iterable/array-like
- Can map values during creation
- Doesn’t modify input
const str = 'hello';
const arr = Array.from(str); // ['h','e','l','l','o']
✨ Spread `...`
- Spreads elements into new array
- Useful for cloning and merging
- Doesn’t mutate originals
* const a = [1, 2];
* const b = [3, 4];
* const combined = [...a, ...b]; // [1, 2, 3, 4]
📄 Quick Reference
Method | Returns | Example | Mutates Original Array? |
---|---|---|---|
push() |
New length | [1, 2].push(3) → 3 |
✅ |
pop() |
Removed element | [1, 2, 3].pop() → 3 |
✅ |
shift() |
Removed element | [1, 2, 3].shift() → 1 |
✅ |
unshift() |
New length | [2, 3].unshift(1) → 3 |
✅ |
splice() |
Removed elements | [1, 2, 3, 4].splice(1, 2) → [2, 3] |
✅ |
slice() |
New array | [1, 2, 3, 4].slice(1, 3) → [2, 3] |
❌ |
concat() |
New array | [1, 2].concat([3]) → [1, 2, 3] |
❌ |
join() |
String | ['a', 'b'].join('-') → 'a-b' |
❌ |
map() |
New array | [1, 2].map(x => x * 2) → [2, 4] |
❌ |
filter() |
New array | [1, 2].filter(x => x > 1) → [2] |
❌ |
reduce() |
Single value | [1, 2].reduce((a, b) => a + b) → 3 |
❌ |
sort() |
Sorted array | [3, 1, 2].sort() → [1, 2, 3] |
✅ |
reverse() |
Reversed array | [1, 2, 3].reverse() → [3, 2, 1] |
✅ |
includes() |
Boolean | [1, 2, 3].includes(2) → true |
❌ |
find() |
First matching item | [1, 2].find(x => x > 1) → 2 |
❌ |
findIndex() |
Index of first match | [1, 2, 3].findIndex(x => x === 2) → 1 |
❌ |
some() |
Boolean | [1, 2, 3].some(x => x > 2) → true |
❌ |
every() |
Boolean | [1, 2, 3].every(x => x > 0) → true |
❌ |
flat() |
New flattened array | [1, [2, 3]].flat() → [1, 2, 3] |
❌ |
flatMap() |
New mapped & flat | [1, 2].flatMap(x => [x, x*2]) → [1, 2, 2, 4] |
❌ |
fill() |
Modified array | [1, 2, 3].fill(0, 1) → [1, 0, 0] |
✅ |
copyWithin() |
Modified array | [1, 2, 3, 4].copyWithin(1, 2) → [1, 3, 4, 4] |
✅ |