JavaScript Methods
Tags: javascript, webdev
Published: 2022-07-21 15:01:00

**1.includes()**
The includes() method focuses on finding out whether an array (a data structure comprising a list of items, each of which stores several elements in a single variable) holds a specific value within its list, then responds with either true or false. It’s vital to remember that includes() is case sensitive when looking for a specific answer. 

`let text = "Hello world, welcome to the universe.";
let result = text.includes("world");`

**2.some()**
The some() method tests are responsible for finding whether at least one item in the array passes the test conducted by the callback function. The callback function accepts only three responses: the item, the index, and the entire array. 

```
const ages = [3, 10, 18, 20];

ages.some(checkAdult);
function checkAdult(age) {
  return age > 18;
}
```

**3.every()**
The every() method uses a similar process as some(), with one change. The difference here is that all the elements in the array pass the test by the callback function. 

```
const isBelowThreshold = (currentValue) => currentValue < 40;

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

console.log(array1.every(isBelowThreshold));
```

**4.filter()**
The filter() method, a favorite for many developers, works by creating a new array by using elements from pre-existing arrays, using a test done by the callback function. 

```
const ages = [32, 33, 16, 40];
const result = ages.filter(checkAdult);

function checkAdult(age) {
  return age >= 18;
}
```

**5.map()**
The map() method, much like the filter() method, creates a new array using the results from the callback function for each element in the array. The callback function receives one of three arguments: currentValue, index, or array. Much like reduce(), the callback function looks for indexes (the element’s location) of the array with assigned values, including if it’s undefined.  

```
const numbers = [4, 9, 16, 25];
const newArr = numbers.map(Math.sqrt)
```

**6.flatMap()**
The flatMap() method takes a function (a task using a specific keyword) to each element along with the array and then “flattens” (or changes) the result into a new array. This method combines flat() and map() in one function, which is more efficient than inputting two different methods separately. 

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

const flattened = arr1.flatMap(num => num);

console.log(flattened);
```

**7.reduce()**
The reduce() method uses the callback function for each value within the array using the four key arguments: accumulator, currentValue, currentIndex, or array. During the first callback, the accumulator and currentValue will result in initialValue if available; if not, the first value of the array will appear. 

```
const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue, currentValue) => previousValue + currentValue,
  initialValue
);

console.log(sumWithInitial);
```


**8.reverse()**
The reverse() method takes an array and reverses its order. This function takes the first element along with the array and makes it the last, and the last array becomes the first element. 

```
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
```

**9.sort()**
The sort() method, one of the most common, focuses on sorting elements of an array in place and then returns the completed array, set to default in ascending order. This method doesn’t always guarantee perfect results, depending on the task’s complexity.  

```
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
```