JavaScript Array Methods
Tags: javascript, array, webdev, programming
Published: 2021-12-27 12:18:47

## JavaScript Array Methods

An array is a special variable that holds more than one value, whether it is a string, number, or object inside a third bracket. If you have a list of things, you can store them inside an array. The syntax is like this:

_**const array_name = [array_item1, array_item2, array_item3, array_item4];**_

_**const tasks = [‘Assignments’, ‘Homework’, ‘Coding’, ‘Read story books’];**_

So if you have a list of tasks to finish today, you can declare an array and write all the
tasks as it’s values. To get the length of the array, we can write
_**‘const length = tasks.length’;**_ and we’ll get the length of the array, which is 4 in the array tasks.

We can access each task’s individual value. Index numbers represent the position of the task in the array. Index of arrays starts from zero, whatever the number of tasks is there in the array. So if you want to access the 2nd item from the array, you’ll write: _**tasks[2];**_ And the output will be ‘Coding’, cause an array starts counting from zero, and by following that rule the 2nd item becomes ‘Coding’.

We can also get the index number of a specific value. _**tasks.indexOf(‘Coding’)**_ will show the index of the value ‘Coding’ and its index is 2.

Adding or removing an item, or reversing the array is also possible. If you call _**tasks.push(‘Watching movies’);**_ this task will be added at the end of the array, after all
the previous tasks. And if you think, you want to add this task at the beginning of the array, you can also do it by using the ‘unshift()’ method.

_**tasks.unshift(‘Watching movies’);**_ will add the task at the beginning of the array. Similarly to remove an item you have to use the ‘pop()’ method, which will remove the
item placed at the end. And to remove the first item of the array ‘shift()’ method will be
used.

If you want to reverse the array, the reverse method will be used.

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

The ‘concat()’ method is used to join two or more arrays together which returns a new array by joining all the arrays. It doesn’t change the value of those arrays, just joins
those arrays and returns their values together by putting them into a new array. For example: if you have two arrays of summer fruits and winter fruits, then want to join both arrays together; you’ll use the concat method.

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

The ‘every()’ method is used to return a boolean value. It runs a function for each element but doesn’t run the function if the elements are empty. And returns ‘true’ if the condition is true for all elements but if it is false, it returns ‘false’.

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

The slice() method is used to select a specific part of an array and returns the selected items of an array. It doesn’t change the original one. The splice() returns the removed items and changes the original array.

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

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

The find method returns the first item that matches with the searched one, it doesn’t check the other remaining items once it gets the first matched item. The filter method returns all the matched items found in the array.