7 MUST KNOW JAVASCRIPT ARRAY METHODS
Published: 2023-01-03 16:31:46


## 7 MUST KNOW JAVASCRIPT ARRAY METHODS

![](https://cdn-images-1.medium.com/max/2000/1*1LN7ytbaLcEciKco6tXNRg.png)

## **1. map()**

![JavaScript array map method explained](https://cdn-images-1.medium.com/max/2364/1*qbOMt9TbrY-QTZylEGr1gA.png)

The **map()** method creates a new array populated with the results of calling a provided function on every element in the calling array.

let us go through an example where we want to double every element of the array.

![JavaScript array map() method explained](https://cdn-images-1.medium.com/max/2000/1*iAIjSMC8vpYZq-RS9bnxew.png)

## 2.filter()

![JavaScript array filter method explained](https://cdn-images-1.medium.com/max/2360/1*XMdx3oG68F3eEd1Kc_okgA.png)

The **filter()** method creates a new array filled with elements that pass a test provided by a function. It does not change the original array just like the map function.

Let’s see an example for this method to filter 18+ adults from ages array.

![JavaScript array filter() method explained](https://cdn-images-1.medium.com/max/2000/1*ctLTRIR7fVdpIRuMA7eT-w.png)

## 3.find()

![JavaScript array find() method explained](https://cdn-images-1.medium.com/max/2360/1*fzmA9V-nxuLuF6Yo-C1kVw.png)

The **find()** method returns the value of the first element that passes a test. This method returns undefined if no elements are found.

Let’s see an example where we use find function to get age of the youngest adult from a sorted ages array.

![JavaScript array find() method explained](https://cdn-images-1.medium.com/max/2000/1*ClwLyN81AllK39i3cf_aVQ.png)

## 4.findIndex()

![JavaScript array findIndex() method explained](https://cdn-images-1.medium.com/max/2346/1*1uU6W72OQZy6UPvtz-4XlQ.png)

The **findIndex()** method returns the index (position) of the first element that passes a test. This method returns **-1** if no match is found.

Let’s see an example where we use findIndex function to get position index of the youngest adult from a sorted ages array.

![JavaScript array findIndex() method explained](https://cdn-images-1.medium.com/max/2072/1*ZsAC-16xLQI1zFHLrKcc9Q.png)

## 5.fill()

![JavaScript array fill() method explained](https://cdn-images-1.medium.com/max/2622/1*Z2KuSPeeR7gVThhSVKnYZg.png)

The **fill()** method fills specified elements in an array with a value. This method overwrites the original array. The start and end position can be specified. If not, all elements will be filled.

![](https://cdn-images-1.medium.com/max/2636/1*9wO90zLMZ8WaZI9-n4wqYw.png)

## 6.some()

![JavaScript array some() method explained](https://cdn-images-1.medium.com/max/2372/1*aUzruPdwIIX9wMe95Zu7ng.png)

The **some()** method checks if any array elements pass a test (provided as a callback function). This method returns true (and stops) if the function returns true for one of the array elements. This method returns false if the function returns false for all of the array elements.

Let’s go through an example where we check for some faulty packets in an array of packet weights. The packet with weight less than 10 is faulty.

![JavaScript array some() method explained](https://cdn-images-1.medium.com/max/2258/1*0er4r-HV5upg1alUVrf-yw.png)

## 7.every()

![JavaScript array every() method explained](https://cdn-images-1.medium.com/max/2362/1*j6CpmHJWojl5Bil13mqaoQ.png)

The **every()** method returns true if the function returns true for all elements. This method returns false if the function returns false for one element.

Let’s go through an example where we check for every packet in an array of packet weights. The packet with weight less than 10 is faulty. The output comes as false because a packet with weight equal to 9 is present in the array.

![JavaScript array every() method explained](https://cdn-images-1.medium.com/max/2358/1*tQ5QSwoZaoQnHIv1rwB7Lg.png)