JavaScript Array Methods Cheatsheet
1. Mutator methods
push(): Adds one or more elements to the end of an array and returns the new length of the array.
let arr = [1, 2, 3];
arr.push(4, 5); // returns 5
// arr is now [1, 2, 3, 4, 5]
pop(): Removes the last element from an array and returns that element.
let arr = [1, 2, 3];
let last = arr.pop(); // returns 3
// arr is now [1, 2]
shift(): Removes the first element from an array and returns that element.
let arr = ["a", "b", "c"];
let first = arr.shift(); // returns "a"
// arr is now ["b", "c"]
unshift(): Adds one or more elements to the beginning of an array and returns the new length of the array.
let arr = [1, 2, 3];
arr.unshift(0); // returns 4
// arr is now [0, 1, 2, 3]
splice(): Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
let arr = [1, 2, 3, 4, 5];
arr.splice(2, 0, "a", "b"); // returns [], no elements removed
// arr is now [1, 2, "a", "b", 3, 4, 5]
reverse(): Reverses the order of the elements of an array in place.
let arr = [1, 2, 3];
arr.reverse();
// arr is now [3, 2, 1]
sort(): Sorts the elements of an array in place and returns the array.
let arr = [1, 3, 2];
arr.sort();
// arr is now [1, 2, 3]
2. Accessor methods
concat(): Returns a new array that is this array joined with other array(s) and/or value(s).
let arr = [1, 2, 3];
let newArr = arr.concat([4, 5]);
// newArr is [1, 2, 3, 4, 5]
join(): Joins all elements of an array into a string.
let arr = ["Hello", "world"];
let str = arr.join(" ");
// str is "Hello world"
slice(): Extracts a section of the calling array and returns a new array.
let arr = [1, 2, 3, 4, 5];
let newArr = arr.slice(1, 3);
// newArr is [2, 3]
indexOf(): Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found.
let arr = [1, 2, 3, 4, 5];
let index = arr.indexOf(3);
// index is 2
lastIndexOf(): Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found.
let arr = [1, 2, 3, 2,
1];
let index = arr.lastIndexOf(2);
// index is 3
3. Iteration methods
forEach(): Executes a provided function once per array element.
let arr = [1, 2, 3];
arr.forEach(item => console.log(item));
// outputs 1, 2, 3
map(): Creates a new array with the results of calling a provided function on every element in this array.
let arr = [1, 2, 3];
let newArr = arr.map(item => item * 2);
// newArr is [2, 4, 6]
filter(): Creates a new array with all elements that pass the test implemented by the provided function.
let arr = [1, 2, 3, 4, 5];
let newArr = arr.filter(item => item > 3);
// newArr is [4, 5]
reduce(): Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce((total, value) => total + value, 0);
// sum is 15
some(): Tests whether some element in the array passes the test implemented by the provided function.
let arr = [1, 2, 3, 4, 5];
let hasLargeNumber = arr.some(item => item > 4);
// hasLargeNumber is true
every(): Tests whether all elements in the array pass the test implemented by the provided function.
let arr = [1, 2, 3, 4, 5];
let allGreaterThanZero = arr.every(item => item > 0);
// allGreaterThanZero is true
find(): Returns the value of the first element in the array that satisfies the provided testing function.
let arr = [1, 2, 3, 4, 5];
let firstLargeNumber = arr.find(item => item > 3);
// firstLargeNumber is 4
findIndex(): Returns the index of the first element in the array that satisfies the provided testing function.
let arr = [1, 2, 3, 4, 5];
let firstLargeNumberIndex = arr.findIndex(item => item > 3);
// firstLargeNumberIndex is 3
includes(): Determines whether an array includes a certain element, returning true or false as appropriate.
let arr = [1, 2, 3, 4, 5];
let includesThree = arr.includes(3);
// includesThree is true