Array methods in javascript
The array object in javascript comes with some built in methods that are super useful when you work with arrays. To understand them better and always keep them in mind, let’s go through the most used ones and learn a bit more about them.
Push( )
The push()
method adds one or more elements to the end of an array and returns the new length of the array.
Pop( )
The pop()
method removes the last element from an array and returns that element. This method changes the length of the array.
shift ( )
The shift()
method removes the first element from an array and returns that removed element. This method changes the length of the array.
unshift( )
The unshift()
method adds one or more elements to the beginning of an array and returns the new length of the array.
toString( )
The toString()
method returns a string representing the specified array and its elements.
join( )
The join()
method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.
splice( )
The splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
slice( )
The slice()
method returns a shallow copy of a portion of an array into a new array object selected from start
to end
(end
not included) where start
and end
represent the index of items in that array. The original array will not be modified.
sort( )
The sort()
method sorts the elements of an array in place and returns the sorted array. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.
map( )
The map()
method creates a new array populated with the results of calling a provided function on every element in the calling array.
filter( )
The filter()
method creates a new array with all elements that pass the test implemented by the provided function.
reduce( )
The reduce()
method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
concat( )
The concat()
method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
every( )
The every()
method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
some( )
The some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
fill( )
The fill()
method changes all elements in an array to a static value, from a start index (default 0
) to an end index (default array.length
). It returns the modified array.
find( )
The find()
method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined
is returned.
findIndex( )
The findIndex()
method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1
, indicating that no element passed the test.
indexOf()
The indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
flat( )
The flat()
method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
forEach( )
The forEach()
method executes a provided function once for each array element.
includes( )
The includes()
method determines whether an array includes a certain value among its entries, returning true
or false
as appropriate.
lastIndexOf( )
The lastIndexOf()
method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex
.
reverse ( )
The reverse()
method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.