Array Methods in JavaScript

Array Methods in JavaScript

·

4 min read

In JavaScript, the type of an array is an object and all the functions in that object(Methods are functions stored as object properties.) are called methods, so we are using some functions inside an object.

1. Push():

If we want to add new elements at the end of an array then we use this method.

2. Unshift():

If we want to add elements at the start of an array then use this method.

3. Pop():

If we want to remove the last element from an array then use it.

4. Shift():

It is used to remove the first element of an array. It will reduce the size of an array.

5. Slice():

It returns an array and this array stores the portion of an original array, like if there are 5 elements in an original array then if we want the elements indexed from 2nd to 4th and in the array form then we use this method, we mention starting and ending index in this method but end index is excluded means it does not return that end indexed element. The original array won't be changed due to this method.

6. Splice():

This method changes the original array by adding or removing some elements from an array. In this method, there are four parameters:

  • starting index: It tells from this index we are to add or remove elements. It can be negative(which means we need to start from the end of the array).

  • Delete Count (optional): It specifies how many elements we want to delete from the start index.

  • Items(optional): Here we insert the items which we want to add to our original array.

7. Includes():

It returns boolean values like true or false, it is used to check whether there is an element at that particular index or whether an array consists of a particular element or not.

8. IndexOf():

If we want the first index of an element in an array then we use this, this returns the first index of that element and if that element is not there then it returns -1. We can use the parameter fromindex to find the index of that element from a particular index.

9. LastIndexOf():

If there are repetitive elements at different indexes and we want only the last index of that element then we use this method.

10. Concat():

This method merges two or more different arrays and this method does not change the existing arrays, but instead returns a new array.

11. Sort():

This method is used to sort the elements of the array, by default it sorts in ascending order.

12. Reverse():

It reverses the elements of an array, which means the last element becomes first now, this changes the original array.

13. Array.isArray(value):

This method is used to determine whether the value passed in this method is an array or not.

14. Length:

It is used to find the number of elements in an array or find the size of an array.

15. Join():

This method is used to return a string by concatenating the all elements of an array, we can use any separator between the elements.

16. Split():

It is just the opposite of the join() method, here we have a string and it converts this into an array, and the elements are the words, characters or whole sentence given what separator we are using.

17. Fill():

This method is used to change the elements of an array with some value, we give three parameters in this method, one for the value which we want to change with elements, the second parameter is starting index from which index fill method is used, and the last parameter is end index - till which index it uses this method. It changes the original array.

18. Map():

This method returns a new array, if we want to apply a certain function to all the elements of an original array then we can use this method, this will apply the function to all elements and store the result as the new elements for the new array and this method returns this new array.