JavaScript methods

Array, String, Object, Number, Map, Set, Promise, Date and the global functions. Every entry with a LIVE pill has a live in-browser demo.

Methods on Array.prototype. Some mutate the array in place, others return a new one — the split that causes most Array bugs.

LIVE
Array.prototype.at
at(index)
Read by index, including NEGATIVE ones — arr[-1] does not work, arr.at(-1) does.
LIVE
Array.prototype.concat
concat(...values)
Join arrays into a NEW one — array arguments are spread, other values appended.
LIVE
Array.prototype.copyWithin
copyWithin(target[, start[, end]])
Shuffle a block of elements to another position in the same array — length never changes.
LIVE
Array.prototype.every
every(callback[, thisArg])
Do ALL elements pass? An empty array answers TRUE — vacuous truth.
LIVE
Array.prototype.fill
fill(value[, start[, end]])
Overwrite a range with one value IN PLACE — and every slot gets the SAME reference.
LIVE
Array.prototype.filter
filter(callback[, thisArg])
Build a NEW array of the elements that pass a test — the length can shrink.
LIVE
Array.prototype.find
find(callback[, thisArg])
The first element that passes a test, or undefined — not an array.
LIVE
Array.prototype.findIndex
findIndex(callback[, thisArg])
Position of the first element that passes a test, or -1.
LIVE
Array.prototype.findLast
findLast(callback) | array.findLastIndex(callback)
find and findIndex, searching from the END instead of the start.
LIVE
Array.prototype.flat
flat([depth])
Flatten nested arrays — ONE level by default, not all the way down.
LIVE
Array.prototype.flatMap
flatMap(callback[, thisArg])
map then flatten ONE level — and returning [] drops the element entirely.
LIVE
Array.prototype.forEach
forEach(callback[, thisArg])
Run a function for each element — returns undefined, and you cannot break out.
LIVE
Array.from
from(source[, mapFn[, thisArg]])
Build a real array from anything iterable or array-like — with an optional map built in.
LIVE
Array.fromAsync
fromAsync(source[, mapFn[, thisArg]])
Array.from for async iterables — and the await-in-order counterpart to Promise.all.
LIVE
Array.prototype.includes
includes(searchElement[, fromIndex])
Is this value in the array? Unlike indexOf, it can find NaN.
LIVE
Array.prototype.indexOf
indexOf(searchElement[, fromIndex])
Position of the first match, or -1 — and it can never find NaN.
LIVE
Array.isArray
isArray(value)
The only reliable array check — typeof says "object" for arrays and everything else.
LIVE
Array.prototype.entries, keys and values
entries() | array.keys() | array.values()
The three iterator methods — lazy, one-shot, and mostly useful with for...of.
LIVE
Array.prototype.join
join([separator])
Concatenate elements into a string — null and undefined become EMPTY, not "null".
LIVE
Array.prototype.lastIndexOf
lastIndexOf(searchElement[, fromIndex])
indexOf from the right — the LAST match, or -1.
LIVE
Array.prototype.map
map(callback[, thisArg])
Build a NEW array by transforming every element — same length, always.
LIVE
Array.of
of(...values)
Array.of(3) is [3]; Array(3) is three empty slots. That inconsistency is why it exists.
LIVE
Array.prototype.pop
pop()
Remove and return the LAST element — undefined on an empty array, never an error.
LIVE
Array.prototype.push
push(...items)
Append to the end IN PLACE — and it returns the new LENGTH, not the array.
LIVE
Array.prototype.reduce
reduce(callback[, initialValue])
Collapse an array to a single value — and the reason to always pass an initial value.
LIVE
Array.prototype.reduceRight
reduceRight(callback[, initialValue])
reduce running backwards — which only matters when the operation is not commutative.
LIVE
Array.prototype.reverse
reverse()
Reverses IN PLACE — the original array is changed, not copied.
LIVE
Array.prototype.shift
shift()
Remove and return the FIRST element — O(n), because everything else moves down.
LIVE
Array.prototype.slice
slice([start[, end]])
Copy a range into a NEW array — end is exclusive, negatives count from the back.
LIVE
Array.prototype.some
some(callback[, thisArg])
Does ANY element pass the test? Short-circuits, and empty is always false.
LIVE
Array.prototype.sort
sort([compareFn])
Sorts IN PLACE, and by default compares as STRINGS — so [10, 9, 1] becomes [1, 10, 9].
LIVE
Array.prototype.splice
splice(start[, deleteCount[, ...items]])
Remove and insert IN PLACE — and it returns what it REMOVED, not what is left.
LIVE
Array.prototype.toReversed
toReversed()
reverse without mutating — a new array, source untouched.
LIVE
Array.prototype.toSorted
toSorted([compareFn])
sort without mutating — but it keeps the default STRING comparison.
LIVE
Array.prototype.toSpliced
toSpliced(start[, skipCount[, ...items]])
splice without mutating — and it returns the RESULTING array, not the removed elements.
LIVE
Array.prototype.toString
toString()
The comma-joined string every array silently becomes — and it flattens nesting completely.
LIVE
Array.prototype.unshift
unshift(...items)
Add to the FRONT in place — returns the new length, and costs O(n).
LIVE
Array.prototype.with
with(index, value)
A copy with one index replaced — and it THROWS on an out-of-range index.