JavaScript Array Methods Cheatsheet
javascriptAll JS array methods with signatures and examples.
Mutation Methods
array.push(element1, ...elementN): numberAdds one or more elements to the end of an array and returns the new length
const arr = [1, 2];
arr.push(3, 4); // returns 4
// arr is now [1, 2, 3, 4]array.pop(): T | undefinedRemoves the last element from an array and returns that element
const arr = [1, 2, 3];
arr.pop(); // returns 3
// arr is now [1, 2]array.shift(): T | undefinedRemoves the first element from an array and returns that element
const arr = [1, 2, 3];
arr.shift(); // returns 1
// arr is now [2, 3]array.unshift(element1, ...elementN): numberAdds one or more elements to the beginning of an array and returns the new length
const arr = [2, 3];
arr.unshift(0, 1); // returns 4
// arr is now [0, 1, 2, 3]array.splice(start, deleteCount?, ...items): T[]Changes array contents by removing or replacing existing elements and/or adding new elements
const arr = [1, 2, 3, 4];
arr.splice(1, 2, 'a', 'b'); // returns [2, 3]
// arr is now [1, 'a', 'b', 4]array.sort(compareFn?): T[]Sorts the elements of an array in place and returns the sorted array
const arr = [3, 1, 4, 1, 5];
arr.sort((a, b) => a - b);
// arr is now [1, 1, 3, 4, 5]array.reverse(): T[]Reverses an array in place and returns the reversed array
const arr = [1, 2, 3];
arr.reverse();
// arr is now [3, 2, 1]array.fill(value, start?, end?): T[]Fills all elements from a start index to an end index with a static value
const arr = [1, 2, 3, 4];
arr.fill(0, 1, 3);
// arr is now [1, 0, 0, 4]array.copyWithin(target, start?, end?): T[]Shallow copies part of an array to another location in the same array without modifying its length
const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3);
// arr is now [4, 5, 3, 4, 5]Access Methods
array.slice(start?, end?): T[]Returns a shallow copy of a portion of an array into a new array
const arr = [1, 2, 3, 4, 5];
arr.slice(1, 4); // returns [2, 3, 4]array.concat(...items): T[]Merges two or more arrays and returns a new array
const arr1 = [1, 2];
const arr2 = [3, 4];
arr1.concat(arr2); // returns [1, 2, 3, 4]array.join(separator?): stringJoins all elements of an array into a string with a specified separator
const arr = ['a', 'b', 'c'];
arr.join('-'); // returns 'a-b-c'array.indexOf(searchElement, fromIndex?): numberReturns the first index at which a given element can be found, or -1 if not present
const arr = [1, 2, 3, 2];
arr.indexOf(2); // returns 1array.lastIndexOf(searchElement, fromIndex?): numberReturns the last index at which a given element can be found, or -1 if not present
const arr = [1, 2, 3, 2];
arr.lastIndexOf(2); // returns 3array.includes(searchElement, fromIndex?): booleanDetermines whether an array includes a certain value among its entries
const arr = [1, 2, 3];
arr.includes(2); // returns truearray.find(callbackFn, thisArg?): T | undefinedReturns the first element in the array that satisfies the provided testing function
const arr = [5, 12, 8, 130];
arr.find(x => x > 10); // returns 12array.findIndex(callbackFn, thisArg?): numberReturns the index of the first element that satisfies the provided testing function, or -1
const arr = [5, 12, 8, 130];
arr.findIndex(x => x > 10); // returns 1array.findLast(callbackFn, thisArg?): T | undefinedReturns the last element in the array that satisfies the provided testing function
const arr = [5, 12, 8, 130];
arr.findLast(x => x > 10); // returns 130array.findLastIndex(callbackFn, thisArg?): numberReturns the index of the last element that satisfies the provided testing function, or -1
const arr = [5, 12, 8, 130];
arr.findLastIndex(x => x > 10); // returns 3array.at(index): T | undefinedReturns the element at the given index, supporting negative indices for counting from the end
const arr = [1, 2, 3, 4];
arr.at(-1); // returns 4Iteration Methods
array.forEach(callbackFn, thisArg?): voidExecutes a provided function once for each array element
const arr = [1, 2, 3];
arr.forEach(x => console.log(x * 2));
// logs: 2, 4, 6array.map(callbackFn, thisArg?): U[]Creates a new array populated with the results of calling a provided function on every element
const arr = [1, 2, 3];
arr.map(x => x * 2); // returns [2, 4, 6]array.filter(callbackFn, thisArg?): T[]Creates a new array with all elements that pass the test implemented by the provided function
const arr = [1, 2, 3, 4, 5];
arr.filter(x => x > 2); // returns [3, 4, 5]array.reduce(callbackFn, initialValue?): UExecutes a reducer function on each element, resulting in a single output value
const arr = [1, 2, 3, 4];
arr.reduce((acc, cur) => acc + cur, 0);
// returns 10array.reduceRight(callbackFn, initialValue?): UApplies a function against an accumulator from right to left to reduce it to a single value
const arr = ['a', 'b', 'c'];
arr.reduceRight((acc, cur) => acc + cur);
// returns 'cba'array.some(callbackFn, thisArg?): booleanTests whether at least one element in the array passes the provided function test
const arr = [1, 2, 3, 4];
arr.some(x => x > 3); // returns truearray.every(callbackFn, thisArg?): booleanTests whether all elements in the array pass the provided function test
const arr = [1, 2, 3, 4];
arr.every(x => x > 0); // returns truearray.flat(depth?): T[]Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth
const arr = [1, [2, [3, [4]]]];
arr.flat(2); // returns [1, 2, 3, [4]]array.flatMap(callbackFn, thisArg?): U[]Maps each element using a mapping function, then flattens the result into a new array by one level
const arr = [1, 2, 3];
arr.flatMap(x => [x, x * 2]);
// returns [1, 2, 2, 4, 3, 6]Creation Methods
Array.from(arrayLike, mapFn?, thisArg?): T[]Creates a new shallow-copied Array instance from an array-like or iterable object
Array.from('abc'); // returns ['a', 'b', 'c']
Array.from([1, 2], x => x * 2); // returns [2, 4]Array.of(...elements): T[]Creates a new Array instance from a variable number of arguments regardless of type
Array.of(1, 2, 3); // returns [1, 2, 3]
Array.of(7); // returns [7]Array.isArray(value): booleanDetermines whether the passed value is an Array
Array.isArray([1, 2, 3]); // returns true
Array.isArray('abc'); // returns falsenew Array(length?) | new Array(...elements): T[]Creates a new Array instance with optional length or initial elements
new Array(3); // returns [empty × 3]
new Array(1, 2, 3); // returns [1, 2, 3][element0, element1, ...elementN]Creates an array using bracket notation with optional initial elements
const arr = [1, 2, 3];
const empty = [];