Skip to content

JavaScript Array Methods

These methods do not modify the original array — they return a new array or value.

MethodReturnsUse Case
.map()New arrayTransform every element
.filter()New arrayKeep elements that pass a test
.reduce()Single valueAccumulate elements into one value
.slice()New arrayExtract a portion of an array
.concat()New arrayMerge two or more arrays
.includes()BooleanCheck if element exists
.find()Element or undefinedFind first element matching a condition
.findIndex()Index or -1Find index of first matching element
.every()BooleanCheck if ALL elements pass a test
.some()BooleanCheck if ANY element passes a test
.flat()New arrayFlatten nested arrays
.flatMap()New arrayMap then flatten one level
const arr = [1, 2, 3, 4, 5];
// map — transform each element
const doubled = arr.map(x => x * 2); // [2, 4, 6, 8, 10]
// filter — keep elements passing condition
const evens = arr.filter(x => x % 2 === 0); // [2, 4]
// reduce — accumulate
const sum = arr.reduce((acc, x) => acc + x, 0); // 15
// slice — extract portion
const mid = arr.slice(1, 4); // [2, 3, 4]
// find — first element matching
const firstEven = arr.find(x => x % 2 === 0); // 2
// some / every
arr.some(x => x > 3); // true
arr.every(x => x > 0); // true

These methods modify the original array in place.

MethodEffectNotes
.push()Add to endReturns new length
.pop()Remove from endReturns removed element
.shift()Remove from startReturns removed element
.unshift()Add to startReturns new length
.splice()Add/remove at indexSwiss Army knife of mutations
.reverse()Reverse in-placeMutates original
.sort()Sort in-placeDefaults to string sort!
.fill()Fill with valueOverwrites elements
const arr = [1, 2, 3, 4, 5];
// push / pop — stack operations
arr.push(6); // [1, 2, 3, 4, 5, 6]
arr.pop(); // [1, 2, 3, 4, 5]
// shift / unshift — queue operations
arr.shift(); // [2, 3, 4, 5]
arr.unshift(1); // [1, 2, 3, 4, 5]
// splice — most versatile
arr.splice(2, 1); // Remove 1 element at index 2 → [1, 2, 4, 5]
arr.splice(2, 0, 3); // Insert 3 at index 2 → [1, 2, 3, 4, 5]
// sort with comparator (IMPORTANT!)
const nums = [10, 5, 20];
nums.sort(); // ❌ [10, 20, 5] — string sort!
nums.sort((a, b) => a - b); // ✅ [5, 10, 20] — numeric sort

Splice vs Slice — VERY COMMON Interview Question

Section titled “Splice vs Slice — VERY COMMON Interview Question”
Featuresplice()slice()
Mutates?✅ YES (modifies original)❌ NO (returns new)
Parameters(start, deleteCount, ...items)(start, end)
ReturnsRemoved elementsExtracted elements
PurposeAdd/remove elementsCopy/extract elements
const arr = [1, 2, 3, 4, 5];
// slice — just copies
const copy = arr.slice(1, 3); // [2, 3] — arr unchanged
// splice — modifies original
const removed = arr.splice(1, 2); // removes [2, 3], arr is now [1, 4, 5]
  1. map — transform data
  2. filter — filter data
  3. reduce — aggregate data
  4. splice vs slice — understand the difference
  5. sort with comparator — numeric sorting
  6. find vs filter — first match vs all matches
  7. some vs every — any vs all condition

Next: Map & Set Patterns →