Skip to main content

Command Palette

Search for a command to run...

08 Array Methods You Must Know

Published
5 min read
08 Array Methods You Must Know

In this blog we are going to cover the important methods that are there in the array , in JS arrays are having some inbuilt methods which are very helpful and frequently used like push() , pop() , shift() , unshift(), map() and much more.

So in this blog what we are going to do is first we will understand the definition of the method and then we will see some examples of it, so let's proceed with the same

push & pop

these methods are used to add or remove the element from the end of the array.

  • push → Insert the element on the end of array

  • pop → remove the element from the end of array

Example :

shift & unshift

these methods are just opposite of the push and pop , here instead of the end we are inserting and removing the element from the front of the array.

  • shift → used to remove the element from the begnning

  • unshift → used to insert the element in the beginning of the array

Example :

Map

map is the method in the array which is used in such case when we want to perform some operations on the each element of the array , Suppose you have an array of numbers and you want to increment each value by 10 , then in this case you can use the map to perform your task easily, one thing that we have to keep in mind is map return new array and doesnot modify the original array.

Syntax of map functions : basically it runs an callback function to modify the values

array.map(callback(element, index, array), thisArg)

As you can see the callback function requires 3 parameters

  1. element - The current element being processed in the array

  2. index(optional) - The index of the current element being processed in the array.

  3. array(optional) - The array map() was called upon.

apart from these parameter one more thing is there that is thisArg which is a value to use as this when executing callbackFn.

Example:

let's take one example of thisArg as well

  • Look at the above example , what i have done is i have created one object myObj and inside that i have one key value: 10 , then I have used map and on the parameter of the map i have passed the object myObj and inside function I have accessed the value using this keyword, and as a result our array gets modified to new array.

Filter

filter is the method which is used to filter out the required value from the array , as the name itself is defining, it also takes callback function as an argument , and that callback function returns the boolean value, if the element is there it should returns true means the element should be included in the new filtered array , otherwise it should return false means the element will be excluded , and one thing that we have to keep in mind is filter() return a new array which includes the filtered element

array.filter(callback(element, index, array))

key point -

  • it returns new array

  • if callback return true - include the value

  • if callback return false - exclude the value

Example:

  • Look at the above example , i have taken one array of numbers and I am filtering out those numbers which are divisible by 2 and hence you can see the results , i got the numbers which are divisible by 2. ( [2, 4 , 6] here)

reduce

as the name suggests , this method is used to reduce an array into single value. like calculating sum, product etc.

array.reduce(callback(accumulator, currentValue, index, array), initialValue)

key points -

  • accumulator → it stores the value of previous iteration.

  • currentValue →current element

  • initialValue → it stores the initial value of the accumulator

Example:

Exaplaination →

  • acc = 1 → 1*1 = 1 → 1*2 = 2 → 2*3 = 6 → 6*4 = 24 → 24*5 = 120 → 120*6 = 720

  • result = 720

forEach

this method is used to iterate over the array and alow us to perform some operation, it also takes callback function as a argument, main thing to keep in mind it doesnot return new array instead it mutates the original array.

Example :

Conclusion :

In this post we've looked at some of the most commonly used array operations in JavaScript , how to add and remove elements at the end (push, pop) and at the beginning (shift, unshift), and how to transform arrays element-by-element with map. These methods form the foundation for manipulating collections and are essential in everyday JS programming.


Hope you found it useful❤️