Methods On An Array
Published: 2022-07-22 09:32:48

What is an array?

An array is an object, it is a data structure that is used to store a bunch of elements. Arrays can consist of numbers, booleans, strings, objects and even other arrays. An array can be created with the following syntax -

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8lux1gyctqu408y24te.png)

Let us discuss different methods that can be used to manipulate array’s -

1) **push()** - This method can be used to insert an element to an array. This method adds and element to the end of an array. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sqp1lfpfl1ons0fenjxg.png)

2) **unshift()** - This method can be used to insert an element to an array. This method adds and element to the beginning of an array. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vhq70rzbcpu5x2b008b9.png)

3) **pop()** - This method allows us to remove an element of an array. This method pops of the last element of the given array. This method returns to us the element that has been removed while also manipulating the original array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e3et8x8jnltpqj0zhpdg.png)

4) **shift()** - This method is like the pop method except that this method allows us to remove the first item of an array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ev2xzfd0vjvc6fzz5mrq.png)

5) **slice()** - This is a method on an array that can be used to return selected elements in an array. This method can also be used to clone an array as it returns a new array instead of modifying the original array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yyay9q9w5911byakc9vl.png)

6) **length** - This method on an array can be used to check the number of elements present in the array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lw0pa111m2z88w8vbccu.png)

7) **concat()** - This method allows us to join two or more arrays. This method returns to us a new array of the merged arrays. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlmu4o6juqg91d9qvq5m.png)

8) **join()** - This method allows us to join all the elements of an array. This method returns to us a string of all the elements in the array that have been separated by a separator, the default separator is a comma. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5xribyfwidagasu8l30.png)

9) **fill()** - This method on an array allows us to replace all or few selected items in the array with a static value. This method manipulates the original array. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xlbdlp7dqd2bw825nh2.png)

10) **includes()** - This method allows us to check whether or not an element is present in a given array. This method returns to us either true or false.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ahwill6uy9skv6i2wlck.png)

11) **indexOf()** - Using this method we can determine the index of any given element in an array, it returns to us the index of the element.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ilziijs7svd52w1wg8ia.png)

12) **reverse()** - This method allows us to reverse the order of elements in an array. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/szru9s1eqz6oifrs1lqe.png)

13) **sort()** - This method converts all the elements of an array into strings and sorts them out, the default sorting method is ascending. This methods manipulates and changes the original array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b1cbn0mutdq2bibaythr.png)

14) **splice()** - This method helps us update, add or delete items from an array. This method manipulates the original array.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ibzjrekg910ksqjf1rfz.png)

15) **map()** - This methods allows us to run some logic for each and every element in the array. This method returns to us a new array after applying the logic to the elements.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j37zqs3aqxs0n4v6iih7.png)

With this we come to an end of this blog, but there are several more methods that can be used to manipulate arrays these are just some of the methods out there. Now since we know about all these methods we can use them to manipulate arrays the next time we're coding!