![cover-img](https://project-assets.showwcase.com/1420x/60042/1673619662777-10%2520array%2520methods%2520in%2520javascript%2520you%2520mus.png?type=webp)
10 Most array methods in JavaScript you must know
13 January, 2023
5
5
1
Contributors
1. Introduction
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
2. filter()
2. map()
3. forEach()
4. splice()
5. reduce()
In this example, the `reduce()` method is called on the `number` array and a function is passed as an argument. The function takes two arguments: an accumulator and the current value. The accumulator starts with the initial value of the first element in the array, and the currentValue is the second element in the array.
The function then adds the accumulator and the current value, and the result is stored in the accumulator. This process is repeated for each element in the array, and the final result is the sum of all the elements in the array.
The `reduce()` method is useful for performing operations on an array that return a single value, such as summing or averaging the elements, finding the minimum or maximum value, or flattening an array of arrays. It allows you to iterate over the elements in an array and perform a specific action on each one, and then combine the results in a single value.
6. find()
In this example, the `find()` method is called on the `numbers` array and a function is passed as an argument. The function tests each element in the array to see if it is divisible by 2 with no remainder (i.e., if it is even). The first element that returns `true` is 2, and it is returned by the `find()` method.
The `find()` method is useful when you want to find a single element that meets a certain condition in an array, without having to iterate through the entire array. It's a useful method for searching for a single element that meets a certain condition, without having to iterate over the entire array.
It's worth noting that, `find()` method stops executing the callback function, as soon as it finds the first element that passes the test, it doesn't check any other elements even if they pass the test.
7. flat()
8. concat()
9. sort()
In this example, the `sort()` method is called on the `words` array and a comparison function is passed as an argument. The comparison function compares the length of two elements, if the length of the first element is less than the second, it returns a negative value, if the length of the first element is equal to the second, it returns zero and if the length of the first element is greater than the second, it returns a positive value.
It's worth noting that the `sort()` method modifies the original array, it doesn't create a new one.
The `sort()` method can be useful when you want to sort the elements of an array, it can be used to sort arrays of numbers, strings, or any other type of data. It's a powerful tool for sorting arrays in place in a variety of ways, either in ascending or descending order, or custom order.