DEV Community

Cover image for JavaScript Array Methods
Nafisa Muntaha
Nafisa Muntaha

Posted on

JavaScript Array Methods

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

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

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

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

Image description

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.

Top comments (0)