Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to Cheatsheet
JavaScript Array Methods Cheat Sheet

JavaScript Array Methods Cheat Sheet

Cheatsheet2,348 viewsBy Admin
cheatsheetjavascriptarraymethodscheat

JS Array Methods Cheat Sheet

Every essential JavaScript array method with quick examples.

Transforming

map(fn)      // [1,2].map(x=>x*2) → [2,4]
filter(fn)   // [1,2,3].filter(x=>x>1) → [2,3]
reduce(fn,0) // [1,2,3].reduce((a,b)=>a+b) → 6
flat()       // [1,[2]].flat() → [1,2]

Finding

find(fn)      // first match
findIndex(fn) // index of first match
includes(x)   // true/false
indexOf(x)    // index or -1

Testing

some(fn)   // at least one matches
every(fn)  // all match

Adding/Removing

push(x)    // add to end
pop()      // remove from end
shift()    // remove from start
unshift(x) // add to start
splice(i,n)// remove/insert at index

Other

slice(a,b)  // copy portion (no mutate)
concat(arr) // merge arrays
join(",")   // array → string
reverse()   // reverse (mutates)
sort(fn)    // sort (mutates)

Mutating vs Non-Mutating

MutatesReturns new
push, pop, splice, sort, reversemap, filter, slice, concat

FAQs

map vs forEach?

map returns a new array; forEach returns nothing. More in our cheat sheets and JS guides.

Which methods mutate?

push, pop, shift, unshift, splice, sort, reverse.