Array
Array<T> · T[]
The ordered list. Array<T> holds elements of one type, grows and shrinks on demand, and carries the richest method collection in the standard library. Half the methods below return new arrays; the other half edit in place — knowing which is which is the whole game.
Usage
Literals to build, methods to derive. Reach for the non-mutating variants unless you mean to edit in place.
const nums = [1, 2, 3]; // literal — this one, always
const doubled = nums.map((n) => n * 2); // new array, nums untouched
const last = nums.at(-1); // 3 — from the end
const sorted = [...nums].sort((a, b) => a - b); // copy first: sort mutatesHow it works
An array is an object with a live length. Reading it counts elements; writing it truncates or extends the array, leaving holes where nothing was ever assigned.
Methods split into two families. push, pop, splice, sort, and reverse edit in place — note sort returning this, the same array back. Their ES2023 twins — toSorted, toReversed, toSpliced, and with — return new arrays instead.
Arrays are generic: map and filter derive new element types from your callbacks, and a type-guard predicate lets filter narrow unions for you. Elements compare by reference when they are objects, which is why includes finds NaN but never the object literal you just built.
Instance API
Everything on Array.prototype, grouped by job. T is the element type.
- includes(searchElement: T, fromIndex?: number): boolean
- Whether the element occurs. Uses SameValueZero, so NaN matches. #
- find(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T | undefined
- First element passing the test, or undefined. #
- findIndex(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): number
- Index of the first passing element, or -1. #
- findLast(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T | undefined
- Like find, searching from the end. #
- findLastIndex(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): number
- Like findIndex, searching from the end. #
- map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]
- A new array with each element mapped. The workhorse. #
- filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]
- A new array with only the passing elements. #
- flat<A, D extends number = 1>(this: A, depth?: D): FlatArray<A, D>[]
- Flattens nested arrays to the given depth. #
- flatMap<U>(callback: (value: T, index: number, array: T[]) => U | readonly U[], thisArg?: any): U[]
- Maps, then flattens one level. Made for one-to-many. #
- slice(start?: number, end?: number): T[]
- A shallow copy of a section. Negatives count from the end. #
- reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U
- Folds left into one value. Also overloaded without an initial value. #
- reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U
- Like reduce, folding from the right. #
- forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void
- Runs the callback per element. Returns nothing; prefer for...of for control flow. #
Static API
Called on Array itself. For checking and building arrays rather than working inside them.
- Array.from<T>(iterable: Iterable<T> | ArrayLike<T>): T[]
- Builds an array from anything iterable or array-like. #