Array methods
Every Array method is present, and arrays are full reference values: a length change (push/pop/splice) inside a callee is visible to the caller for every argument shape — a plain variable, an object-field or array-element array, or a HOF-callback element. The remaining recurring caveat is structural: the element-comparing helpers (indexOf/includes/join/sort) reject a nested-array element.
[a, b, c] (literal)
ExactArray literal. Element type is inferred from the first element.
const nums: number[] = [1, 2, 3, 4, 5]new Array<T>(n)
ExactConstructs an array of length n. A length of 0 gives an empty array ready to push into.
let arr = new Array<number>(0)
arr.push(10).length
Works, with caveatsThe number of elements. Read in the usual way.
a.length = 2(real JS's array-truncation idiom) hard compile-errors with "field assignment on non-object" — length is read-only in practice.
.push(...items)
ExactAppends items and returns the new length. Variadic (including the zero-argument call), and works on any mutable receiver — a variable, an object/class array field (this.items.push(x)), or a nested-array element (matrix[0].push(x)).
Returnsnumber — The new length.
let arr = new Array<number>(0)
arr.push(10)
let len: number = arr.push(20) // 2.pop()
ExactRemoves and returns the last element. Works on any mutable receiver (variable, object/class field, nested-array element).
ReturnsT | undefined — The removed element, or `undefined` on an empty array.
let stack = [10, 20, 30]
let a = stack.pop() // 30- The absence result is a real
T | undefined(TDD-00187): strict mode requires narrowing (!== undefined), a??default, or a!assertion before use where a bareTis expected;-compat=jsauto-widens.
.shift()
ExactRemoves and returns the first element, shifting the rest down. Works on any mutable receiver.
ReturnsT | undefined — The removed element, or `undefined` on an empty array.
const done = list.titles.shift()- The absence result is a real
T | undefined(TDD-00187): strict mode requires narrowing (!== undefined), a??default, or a!assertion before use where a bareTis expected;-compat=jsauto-widens.
.unshift(...items)
ExactPrepends items and returns the new length. Variadic (including the zero-argument call), any mutable receiver.
Returnsnumber — The new length.
list.titles.unshift("wake up", "coffee in Thessaloniki").splice(start, delete?, ...items)
ExactRemoves deleteCount elements from start and inserts items in their place. deleteCount clamps to [0, len - start] and a negative start is normalized, matching real JS. Works on any mutable receiver.
ReturnsT[] — The removed elements.
.slice(start?, end?)
ExactReturns a shallow copy of a section of the array (the whole array with no arguments). Negative indices count from the end. Does not mutate the original.
ReturnsT[] — A new array.
.at(i)
ExactReturns the element at index i, with negative indices counting from the end.
ReturnsT | undefined — The element at i, or `undefined` when out of range (negative indices count from the end; one past the start is out of range, matching Node).
- The absence result is a real
T | undefined(TDD-00187/TDD-00221, nestedT[][]included): strict mode requires narrowing (!== undefined), a??default, or a!assertion before use where a bareTis expected;-compat=jsauto-widens.
.indexOf(item, fromIndex?)
Works, with caveatsReturns the index of the first matching element at or after the optional fromIndex (negative counts from the end), or -1.
Returnsnumber — First match index, or -1.
- Rejects a nested-array element (
number[][]) — compares a bare register, no callback.
.includes(item)
Works, with caveatsWhether the array contains the element.
Returnsboolean — true if present.
- Rejects a nested-array element (
number[][]) — compares a bare register, no callback.
.find(fn)
ExactReturns the first element for which the predicate is true.
ReturnsT | undefined — The first matching element, or `undefined` when nothing matches.
const first3 = [1,2,3,4,5].find(x => x > 3) // 4- The absence result is a real
T | undefined(TDD-00187/TDD-00221, nestedT[][]included): strict mode requires narrowing (!== undefined), a??default, or a!assertion before use where a bareTis expected;-compat=jsauto-widens.
.findIndex(fn)
ExactReturns the index of the first element for which the predicate is true, or -1.
Returnsnumber — The index, or -1.
.some(fn)
ExactWhether the predicate is true for at least one element.
Returnsboolean — true if any element matches.
const hasEven = [1,2,3].some(x => x % 2 === 0) // true.every(fn)
ExactWhether the predicate is true for all elements.
Returnsboolean — true if every element matches.
const allPositive = [1,2,3].every(x => x > 0) // true.map(fn)
ExactReturns a new array with the callback applied to each element.
ReturnsU[] — The mapped array.
const doubled = [1,2,3].map(x => x * 2) // [2,4,6].filter(fn)
ExactReturns a new array of the elements for which the predicate is true.
ReturnsT[] — The filtered array.
const evens = [1,2,3,4].filter(x => x % 2 === 0) // [2,4].reduce(fn, init?)
ExactFolds the array to a single value. The initial value is optional — without it the first element seeds the accumulator and the fold starts from the second. An empty array with no initial value throws, matching real JS.
Parameters- fn(acc, x) => U
- Reducer called per element.
- init?U
- Seed accumulator; the first element is used if omitted.
U — The folded result.
const sum = [1,2,3,4,5].reduce((acc, n) => acc + n, 0) // 15.reduceRight(fn, init?)
ExactFolds the array to a single value right-to-left. Like .reduce() but visiting the last element first: without an initial value the last element seeds the accumulator and the fold walks leftward from the second-to-last; an empty array with no initial value throws, matching real JS.
Parameters- fn(acc, x) => U
- Reducer called per element, from the end.
- init?U
- Seed accumulator; the last element is used if omitted.
U — The folded result.
const rtl = ['a','b','c','d'].reduceRight((acc, w) => acc + w, '') // 'dcba'.forEach(fn)
ExactRuns the callback for each element, for its side effects. The callback receives the element and, if declared, the index and the source array (a live view over the same backing).
Returnsvoid — Nothing.
.join(sep?)
ExactJoins the elements into a string, separated by sep (default ","). A nested-array element is rendered as its own comma-joined string (real JS's recursive toString), so [[1,2],[3,4]].join("-") is "1,2-3,4". Shares the array→string coercion path used by String(arr) and `${arr}`.
Returnsstring — The joined string.
console.log(['hello','world'].join(', ')) // hello, world.sort(fn?)
ExactSorts the array in place, stably, with V8's TimSort — the same order as Node for any comparator. With no comparator the default is JS-faithful — elements are stringified and compared lexicographically, so `[10,1,21,2].sort()` is `[1,10,2,21]`; pass a comparator for a numeric sort. A custom comparator returns a negative, zero, or positive number; undefined elements go last.
ReturnsT[] — The array, sorted in place.
const desc: number[] = [3, 1, 4, 1, 5]
desc.sort((a: number, b: number) => b - a) // [5,4,3,1,1].reverse()
ExactReverses the array in place.
ReturnsT[] — The array, reversed in place.
.fill(val, start?, end?)
ExactFills a section of the array with a static value, in place.
ReturnsT[] — The array, filled in place.
.concat(...arrays)
ExactReturns a new array joining the receiver with the given arrays.
ReturnsT[] — The concatenated array.
.flat(depth?)
Works, with caveatsFlattens nested arrays. depth defaults to 1. Infinity flattens as deep as the receiver's own static type nests. Always returns a fresh array, even when nothing gets flattened.
ReturnsT[] — A fresh flattened array.
depthmust be a compile-time constant integer orInfinity— this compiler's arrays have a fixed nesting depth at the type level, so the result's element type has to be known at compile time.
.flatMap(fn)
Exact.map(fn) followed by exactly one level of flattening — like real JS, no depth parameter. A callback that doesn't return an array per element is just a plain map.
ReturnsU[] — The mapped-and-flattened array.
.findLast(fn) / .findLastIndex(fn)
ExactFinds the last matching element / its index. Genuine reverse iteration, not a forward scan keeping the last match — the callback is invoked starting from the last element, matching real JS's reverse call order, observable via side effects.
See it in a full example →- The absence result is a real
T | undefined(TDD-00187/TDD-00221, nestedT[][]included): strict mode requires narrowing (!== undefined), a??default, or a!assertion before use where a bareTis expected;-compat=jsauto-widens.
.toSorted() / .toReversed() / .toSpliced()
ExactNon-mutating counterparts of .sort()/.reverse()/.splice() — sort or reverse a fresh copy, or build a fresh spliced result, leaving the original array untouched.
ReturnsT[] — A fresh array; the receiver is unchanged.
.with(i, val)
ExactReturns a fresh copy with the element at i replaced. Negative indices count from the end like .at(); an index still out of range after normalization throws a catchable Error, matching real JS's RangeError.
ReturnsT[] — A fresh copy with one element replaced.
.keys() / .values() / .entries()
ExactReturn an Array Iterator over the array itself — next(), the iterator helpers (map, filter, take, drop, flatMap, reduce, toArray, forEach, some, every, find), for...of, spread and Array.from — which sees a later push, as in Node. entries() yields [index, value] tuples.
for (const [i, v] of arr.entries()) { /* ... */ }.copyWithin(target, start?, end?)
ExactCopies a section of the array to another position within it, in place. Overlap-safe via memmove — a self-overlapping copy such as arr.copyWithin(0, 3) on a 5-element array is handled correctly.
ReturnsT[] — The array, modified in place.
Array.isArray(x)
ExactWhether the value is an array. Static method on Array.
Returnsboolean — true if x is an array.
Array.from(iterable, mapFn?)
Works, with caveatsBuilds an array from an array, a Set, a Map (as entries), a string (its characters), a generator, any iterable (a [Symbol.iterator]() method, a value held in `any`), a class implementing next(): T | null, or an array-like object (its length, then its indexed properties). An optional mapFn transforms each element by value and index. Static method on Array.
ReturnsT[] | U[] — The constructed array (the mapped type when mapFn is given).
thisArg(the third argument) is not supported.
Array.of(...items)
ExactBuilds an array from its arguments — the array literal [...] of them, usable anywhere one is; mixed argument kinds work as in a mixed literal. Static method on Array.
ReturnsT[] — The constructed array.