Array methods cheatsheet
By: Paul Browne
Tags: Arrays, JavaScript

A bunch of array methods. All examples work in IE9

length

length will simply return the length of the array.

isArray

isArray is a method to test if a variable is an array.

indexOf

indexOf will return the first index at which a given element can be found in the array. It will return -1 if not found. The first index is 0.

You can also specify a starting from index eg. indexOf("hello", 4)

lastIndexOf

lastIndexOf will return the first index at which a given element can be found in the array. lastIndexOf will search from the end to start It will return -1 if not found. The first index is 0. You can also specify a starting from index

push

push will append one or more elements to an array, and return the length of the new array.

unshift

unshift will prepend one or more elements to an array, and return the length of the new array.

pop

pop will remove the last element in an array, and return that element.

shift

shift will remove the first element in an array, and return that element.

join

join will create a single string by joining all of the elements in an array. If no argument is passed, a comma will be used to separate the items. This will also work on nested arrays.

concat

concat will merge two or more arrays. This method will not change the original arrays, but instead creates a new array.

forEach

forEach will basically just a loop over the array, it has no return value. You can also not return or break from within the loop.

filter

filter will loop over the array and perform a test on each element. If the element passes the test, it will be added to the returned array.

map

map will loop over the array, do something to each element, then return the new array.

some

some will perform a test on each element and if one of the elements passes the test, it will return true, otherwise false.

every

every will perform a test on each element and if all of the elements pass the test, it will return true, otherwise false.

reduce

reduce will loop over the array and execute a function with access to the accumulator, the current value, the index and the original array. It will return one value.

reduceRight

reduceRight will loop over the array, from end to start, and execute a function with access to the accumulator, the current value, the index and the original array. It will return one value.

sort

sort will loop over the array, run a function comparing that element with the current contents of the new array and then place that element in the new array at its correct position.

reverse

reverse will reverse the array. The reverse method will reverse the original array, regardless if you write var newArray = array.reverse();

toString

toString will return a string representation of the array

slice

slice will return a copy of a portion of an array into a new array selected from begin to end indices (end not included). The original array will not be affected. negative indices will start from the end of the array.

splice

splice will change the contents of an array by removing or replacing existing elements, as well as possibly adding new elements. It is also possible to assign a variable to the removed elements.