Array Methods
Tags: javascript, beginners
Published: 2020-05-03 13:40:46

---
title: Array Methods
published: true
description: find, add and remove
tags: javascript, beginners
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/by6k7fuay76t2hg6zmd3.jpg
---

With array often we need to find the position of an item (<mark>index</mark>).
For the primitive values we use **IndexOf()** method.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vb7for08pvp57l2x19d1.png)
For function, array and object we use **findIndex()** .We will also se this later.
If we want to add an item in the beginning of an array we do this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8bja4m5s572jn40ky3ti.png)
If we want to add more than one item we do this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mdng5qvxbam7jj4da0u5.png)
We add the other items separated by comma.
Sometimes we have to remove the first item of an array , we can do this in this way:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/os6fd44yzox3h3o1rffa.png)
If we want remove more than one item in the beginning we do this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2dxl63lmua26kdroszr6.png)
In the code above we use shift() method two times to delete two items.
Now we talk about to add element and remove element in the end of an array.
If we want to add an item in the end of an array we can do this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/klbyu3n9yp6g14gnxrm1.png)
Now more than one item:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4jkryul0ajkmuyoukzrb.png)
To delete the last item of an array we may do this:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5chxx6ipkls7znjc3dph.png)
More than one:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3032mo1kn7jg7qf4jr9u.png)
There is many occasions when we want to create a copy of our array to avoid changes in the original, we may perform this with slice() method.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kuk1alguftf70mlduahx.png)
In the end I want to talk about **splice()** that allow us to add, remove item / items from the beginning, the end or in the middle of an array. Is a penknife method.
Anatomy of splice() method:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9nku2kkl5v4yaqhrh64d.png)
Add one or more items in the beginnig of an array:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/49i4tay63ncs85x9f4s9.png)
More than one:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vdla8kjzeeg203em6jh9.png)
Remove one from the beginning:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6s2cmkf4uff7gxvqwpvr.png)
Remove more than one:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/qlli5zd0ijvg9hb47f9z.png)
Add one item in the end:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5d3y73288feast7vgzwf.png)
Add more than one item in the end:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/npr60h6d8gyf6b5h17gr.png)
The last thing that I want to show you is how to add or remove Items from the middle of an array with the splice() method:
Add:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r38sy5o8boem7ibvcusy.png)
Remove:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ak2i3hk1edh9t26hjjz7.png)
And replace:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/akekc13c5irlf9qm1vpx.png)