Javascript Array Methods Atleast You Should Know
Tags: javascript, array
Published: 2020-09-13 13:46:16

> #### map()

_Creates a new array populated with the results of calling a provided function on every element in the calling array._

```
const arr = [1, 2, 3, 4, 5, 6];

// add one to every element
  const oneAdded = arr.map(num => num + 1);
  console.log(oneAdded); 
// output [2, 3, 4, 5, 6, 7]

  console.log(arr); 
// output: [1, 2, 3, 4, 5, 6]
```
Another Sample:
```
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
```

> #### forEach()

_Executes a provided function once for each array element.Help you to loop over array’s items._

```
const arr = [1, 2, 3, 4, 5, 6];

  arr.forEach(item => {
    console.log(item); 
// output: 1 2 3 4 5 6
  });
```
Another Sample:
```
const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"
```

> #### includes()

_Determines whether an array includes a certain value among its entries, returning `true` or `false` as appropriate._

```
const arr = [1, 2, 3, 4, 5, 6];

  arr.includes(2); // output: true
  arr.includes(7); // output: false
```

Another Sample:
```
const array1 = [1, 2, 3];

console.log(array1.includes(2));
// expected output: true

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// expected output: true

console.log(pets.includes('at'));
// expected output: false
```

> #### filter()

_Creates a new array with all elements that pass the test implemented by the provided function_
_Creates an array filled with all array elements that pass a test (provided as a function)_

```
const arr = [1, 2, 3, 4, 5, 6];

  // item(s) greater than 3
  const filtered = arr.filter(num => num > 3);
  console.log(filtered); // output: [4, 5, 6]

  console.log(arr); // output: [1, 2, 3, 4, 5, 6]
```
Another Sample:
```
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
```

> ####  reduce()

_Executes a reducer function (that you provide) on each element of the array, resulting in single output value._
```
const arr = [1, 2, 3, 4, 5, 6];

const sum = arr.reduce((total, value) => total + value, 0);
console.log(sum); 
// 21
```
Another Sample:
```
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
```

> #### some()

_Tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value._

```
const arr = [1, 2, 3, 4, 5, 6];

  // at least one element is greater than 4?
  const largeNum = arr.some(num => num > 4);
  console.log(largeNum); // output: true

  // at least one element is less than or equal to 0?
  const smallNum = arr.some(num => num <= 0);
  console.log(smallNum); 
// output: false
```
Another Sample:
```
const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// expected output: true
```

> #### every()

_Tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value. If passed, it return `true` otherwise `false`._

```
  const arr = [1, 2, 3, 4, 5, 6];

  // all elements are greater than 4
  const greaterFour = arr.every(num => num > 4);
  console.log(greaterFour); // output: false

  // all elements are less than 10
  const lessTen = arr.every(num => num < 10);
  console.log(lessTen); // output: true
```
Another Sample:
```
const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true
```

> #### join()

_Creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator._

```
const elements = ['Fire', 'Air', 'Water'];

console.log(elements.join());
// expected output: "Fire,Air,Water"

console.log(elements.join(''));
// expected output: "FireAirWater"

console.log(elements.join('-'));
// expected output: "Fire-Air-Water"

```