5 Famous Array Methods in JavaScript.
Tags: arraymethods, javascript
Published: 2020-11-24 03:39:48

---
title: 5 Famous Array Methods in JavaScript.
published: true
date: 2019-09-16 15:03:03 UTC
tags: arraymethods,javascript
canonical_url: https://medium.com/@nikhilswain36/5-famous-array-methods-in-javascript-c4b68673178a
---

#### map() , reduce() , filter() , some() , every() .

JavaScript provide us lots of useful methods to ease our work.In this article we will take a closer look at some JavaScript famous **Array methods** that will help us to code efficiently.

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

1. **Array.prototype.map()**  
The map() method creates a **new array** with the results of calling a function for every array element.It accepts a callback function as a parameter and we have access to a ccurrent value of an array, current index and array itself as well.  
**Syntax :** _array_.map(_function(currentValue, index, arr), thisValue_)

```
const array = [2,4,6,8];

const newArr = array.map(val => val*2);

// [4, 8, 12, 16]
```

You could also pass a callback function as a parameter.

```
const callbackFunc = val => val + 2;

const array = [2,4,6,8];

cons newArr = array.map(sum)

// [4, 6, 8, 10]
```

Chaining is also available in so we can do something like this :

```
const sum = val => val + 2;

const mul = val => val * 2;

const array = [1, 2, 3, 4, 5];

const newArray = array.map(sum).map(mul);

// [6, 8, 10, 12, 14]
```

**2. Array.prototype.reduce()**  
The reduce() method executes a **reducer** function (that you provide) on each element of the array, resulting in a single output value.  
The reducer function takes four arguments:

1. Accumulator
2. Current Value
3. Current Index
4. Source Array

Your reducer function’s returned value is assigned to the accumulator, whose value is remembered across each iteration throughout the array and ultimately becomes the final, single resulting value.  
**Syntax :**   
_array_.reduce(_function(total, currentValue, currentIndex, arr), initialValue_)

```
function myFunc( total, num ) {
return total + num ;
}

const numbers = [30, 20, 10];

numbers.reduce(myFunc)
// 60
```

We can also provide an initial value instead of taking the first value as an accumulator by default :

```
const num = [5, 10, 15, 10];

const sum = num.reduce(( acc, val ) => acc + val, 100);

// 140
```

**3. Array.prototype.filter()**  
The filter() method creates a new array with all elements that pass the test implemented by the provided function.  
**Syntax :**   
const newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

callback : Function is a predicate, to test each element of the array. Return true to keep the element, false otherwise.

It accepts three arguments:

_element_ : The current element being processed in the array.

_index :_ (Optional) The index of the current element being processed in the array.

_array_ : (Optional) The array filter was called upon.

_thisArg :_ (Optional) Value to use as this when executing callback.

```
const band = ['Against', 'the', 'current', 'is', 'my', 'favourite', 'band'];


const newBand = band.filter( word => word.length > 5 );

// ["Against", "current", "favourite"]
```

Let’s look at another example.

```
const isGreater = (val) => {
return val > 12;
}

const realNumbers = [3,6,12,15,18,21].filter(isGreater);

// [15, 18, 21]
```

**4. Array.prototype.some()**  
As the name suggest the some() method tests whether at least **one element** in the array passes the test implemented by the provided function. It returns a Boolean value .  
**Syntax :** _array_.some(_function(currentValue, index, arr), thisValue_)

```
const names = [const names = ['sheldon', 'lenoard', 'raj', 'howard', 'penny'];

names.some(having => having === 'sheldon');

// true

names.some(having => having === 'chrissy');

// false
```

Another example of some() method

```
const number = [5,7,10,12,15,17,20];

number.some(mulOfFive => mulOfFive % 5 === 0);
```

**5. Array.prototype.every()**  
Unlike some() the every() method tests whether all elements in the array pass the test implemented by the provided function. It also returns a Boolean value.  
**Syntax :** _array_.every(_function(currentValue, index, arr), thisValue_)

```
const band = ['ATC','ATC','ATC','ATC'];

band.every(having => having === 'ATC');

// true
```

Another example of some() method

```
const marks = [80, 75, 52, 91, 63];

marks.every(mark => mark > 50);

// true
```