{ ILoveJS }

JavaScript Array Methods Cheatsheet

javascript

All JS array methods with signatures and examples.

4 sections · 34 items

Mutation Methods

push()
array.push(element1, ...elementN): number

Adds one or more elements to the end of an array and returns the new length

typescript
const arr = [1, 2];
arr.push(3, 4); // returns 4
// arr is now [1, 2, 3, 4]
pop()
array.pop(): T | undefined

Removes the last element from an array and returns that element

typescript
const arr = [1, 2, 3];
arr.pop(); // returns 3
// arr is now [1, 2]
shift()
array.shift(): T | undefined

Removes the first element from an array and returns that element

typescript
const arr = [1, 2, 3];
arr.shift(); // returns 1
// arr is now [2, 3]
unshift()
array.unshift(element1, ...elementN): number

Adds one or more elements to the beginning of an array and returns the new length

typescript
const arr = [2, 3];
arr.unshift(0, 1); // returns 4
// arr is now [0, 1, 2, 3]
splice()
array.splice(start, deleteCount?, ...items): T[]

Changes array contents by removing or replacing existing elements and/or adding new elements

typescript
const arr = [1, 2, 3, 4];
arr.splice(1, 2, 'a', 'b'); // returns [2, 3]
// arr is now [1, 'a', 'b', 4]
sort()
array.sort(compareFn?): T[]

Sorts the elements of an array in place and returns the sorted array

typescript
const arr = [3, 1, 4, 1, 5];
arr.sort((a, b) => a - b);
// arr is now [1, 1, 3, 4, 5]
reverse()
array.reverse(): T[]

Reverses an array in place and returns the reversed array

typescript
const arr = [1, 2, 3];
arr.reverse();
// arr is now [3, 2, 1]
fill()
array.fill(value, start?, end?): T[]

Fills all elements from a start index to an end index with a static value

typescript
const arr = [1, 2, 3, 4];
arr.fill(0, 1, 3);
// arr is now [1, 0, 0, 4]
copyWithin()
array.copyWithin(target, start?, end?): T[]

Shallow copies part of an array to another location in the same array without modifying its length

typescript
const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3);
// arr is now [4, 5, 3, 4, 5]

Access Methods

slice()
array.slice(start?, end?): T[]

Returns a shallow copy of a portion of an array into a new array

typescript
const arr = [1, 2, 3, 4, 5];
arr.slice(1, 4); // returns [2, 3, 4]
concat()
array.concat(...items): T[]

Merges two or more arrays and returns a new array

typescript
const arr1 = [1, 2];
const arr2 = [3, 4];
arr1.concat(arr2); // returns [1, 2, 3, 4]
join()
array.join(separator?): string

Joins all elements of an array into a string with a specified separator

typescript
const arr = ['a', 'b', 'c'];
arr.join('-'); // returns 'a-b-c'
indexOf()
array.indexOf(searchElement, fromIndex?): number

Returns the first index at which a given element can be found, or -1 if not present

typescript
const arr = [1, 2, 3, 2];
arr.indexOf(2); // returns 1
lastIndexOf()
array.lastIndexOf(searchElement, fromIndex?): number

Returns the last index at which a given element can be found, or -1 if not present

typescript
const arr = [1, 2, 3, 2];
arr.lastIndexOf(2); // returns 3
includes()
array.includes(searchElement, fromIndex?): boolean

Determines whether an array includes a certain value among its entries

typescript
const arr = [1, 2, 3];
arr.includes(2); // returns true
find()
array.find(callbackFn, thisArg?): T | undefined

Returns the first element in the array that satisfies the provided testing function

typescript
const arr = [5, 12, 8, 130];
arr.find(x => x > 10); // returns 12
findIndex()
array.findIndex(callbackFn, thisArg?): number

Returns the index of the first element that satisfies the provided testing function, or -1

typescript
const arr = [5, 12, 8, 130];
arr.findIndex(x => x > 10); // returns 1
findLast()
array.findLast(callbackFn, thisArg?): T | undefined

Returns the last element in the array that satisfies the provided testing function

typescript
const arr = [5, 12, 8, 130];
arr.findLast(x => x > 10); // returns 130
findLastIndex()
array.findLastIndex(callbackFn, thisArg?): number

Returns the index of the last element that satisfies the provided testing function, or -1

typescript
const arr = [5, 12, 8, 130];
arr.findLastIndex(x => x > 10); // returns 3
at()
array.at(index): T | undefined

Returns the element at the given index, supporting negative indices for counting from the end

typescript
const arr = [1, 2, 3, 4];
arr.at(-1); // returns 4

Iteration Methods

forEach()
array.forEach(callbackFn, thisArg?): void

Executes a provided function once for each array element

typescript
const arr = [1, 2, 3];
arr.forEach(x => console.log(x * 2));
// logs: 2, 4, 6
map()
array.map(callbackFn, thisArg?): U[]

Creates a new array populated with the results of calling a provided function on every element

typescript
const arr = [1, 2, 3];
arr.map(x => x * 2); // returns [2, 4, 6]
filter()
array.filter(callbackFn, thisArg?): T[]

Creates a new array with all elements that pass the test implemented by the provided function

typescript
const arr = [1, 2, 3, 4, 5];
arr.filter(x => x > 2); // returns [3, 4, 5]
reduce()
array.reduce(callbackFn, initialValue?): U

Executes a reducer function on each element, resulting in a single output value

typescript
const arr = [1, 2, 3, 4];
arr.reduce((acc, cur) => acc + cur, 0);
// returns 10
reduceRight()
array.reduceRight(callbackFn, initialValue?): U

Applies a function against an accumulator from right to left to reduce it to a single value

typescript
const arr = ['a', 'b', 'c'];
arr.reduceRight((acc, cur) => acc + cur);
// returns 'cba'
some()
array.some(callbackFn, thisArg?): boolean

Tests whether at least one element in the array passes the provided function test

typescript
const arr = [1, 2, 3, 4];
arr.some(x => x > 3); // returns true
every()
array.every(callbackFn, thisArg?): boolean

Tests whether all elements in the array pass the provided function test

typescript
const arr = [1, 2, 3, 4];
arr.every(x => x > 0); // returns true
flat()
array.flat(depth?): T[]

Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth

typescript
const arr = [1, [2, [3, [4]]]];
arr.flat(2); // returns [1, 2, 3, [4]]
flatMap()
array.flatMap(callbackFn, thisArg?): U[]

Maps each element using a mapping function, then flattens the result into a new array by one level

typescript
const arr = [1, 2, 3];
arr.flatMap(x => [x, x * 2]);
// returns [1, 2, 2, 4, 3, 6]

Creation Methods

Array.from()
Array.from(arrayLike, mapFn?, thisArg?): T[]

Creates a new shallow-copied Array instance from an array-like or iterable object

typescript
Array.from('abc'); // returns ['a', 'b', 'c']
Array.from([1, 2], x => x * 2); // returns [2, 4]
Array.of()
Array.of(...elements): T[]

Creates a new Array instance from a variable number of arguments regardless of type

typescript
Array.of(1, 2, 3); // returns [1, 2, 3]
Array.of(7); // returns [7]
Array.isArray()
Array.isArray(value): boolean

Determines whether the passed value is an Array

typescript
Array.isArray([1, 2, 3]); // returns true
Array.isArray('abc'); // returns false
Array Constructor
new Array(length?) | new Array(...elements): T[]

Creates a new Array instance with optional length or initial elements

typescript
new Array(3); // returns [empty × 3]
new Array(1, 2, 3); // returns [1, 2, 3]
Array Literal
[element0, element1, ...elementN]

Creates an array using bracket notation with optional initial elements

typescript
const arr = [1, 2, 3];
const empty = [];

Related Content