Best-Known JavaScript Array Methods

An array is a special variable in all the languages that is used to store different elements. JavaScript array contains some built-in properties that every JavaScript developer should know how to use them and when or where to use them. We can use them to add, remove, iterate, or manipulate data as per our requirements. There are some basic JavaScript array methods that every developer should know.
1. some() Method: This method checks whether at least one of the elements of the array satisfies the condition checked by the argument function.
- Example:
- Output:
true
<script> // JavaScript to illustrate // lastIndexOf() method function isGreaterThan5(element, index, array) { return element > 5; } function func() { // Original array var array = [2, 5, 8, 1, 4]; // Checking for condition in array var value = array.some(isGreaterThan5); document.write(value); } func(); </script> |
2. reduce() Method: The array reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator.
- Example:
<script>// Original arrayvarnumbers = [88, 50, 25, 10];// Performing reduce methodvarsub = numbers.reduce(geeks);functiongeeks(total, num) {returntotal - num;}document.write(sub)</script>chevron_rightfilter_none - Output:
3
3. map() Method: The map() method in JavaScript create an array by calling a specific function on each element present in the parent array. It is a non-mutating method. Generally, the map() method is used to iterate over an array and calling the function on every element of an array.
- Example:
<script>// Original arrayvarnumbers = [4, 9, 16, 25];// Performing map methodvarsub = numbers.map(geeks);functiongeeks() {returnnumbers.map(Math.sqrt);}document.write(sub)</script>chevron_rightfilter_none - Output:
2, 3, 4, 5
4. every() Method: This method checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.
- Example:
<script>// JavaScript code for every() functionfunctionispositive(element, index, array) {returnelement > 0;}functionfunc() {vararr = [ 11, 89, 23, 7, 98 ];// Check for positive numbervarvalue = arr.every(ispositive);document.write(value);}func();</script>chevron_rightfilter_none - Output:
true
5. flat() Method: This method creates a new array that contains more than arrays. Basically creates a simple array from an array that contains multiple arrays.
- Example:
<script>//Original arrayvararr = [ [11, 89], [23, 7], 98 ];// Performing flat methodvargeeks = arr.flat();document.write(geeks)</script>chevron_rightfilter_none - Output:
11, 89, 23, 7, 98
6. flatMap() Method: This method is used to flatten the input array element into a new array. This method first of all map every element with the help of mapping function, then flattens the input array element into a new array.
- Example:
<script>const myAwesomeArray = [[1], [2], [3], [4], [5]]vargeeks = myAwesomeArray.flatMap(arr => arr * 10)console.log(geeks);</script>chevron_rightfilter_none - Output:
10, 20, 30, 40, 50
7. filter() Method: This method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument function.
- Example:
<script>functionisPositive(value) {returnvalue > 0;}functionfunc() {varfiltered = [112, 52, 0, -1, 944].filter(isPositive);document.write(filtered);}func();</script>chevron_rightfilter_none - Output:
112, 52, 944
8. findindex() Method: This method returns index of the first element in a given array that satisfies the provided testing function. Otherwise -1 is returned.
- Example:
<script>vararray = [ 10, 20, 30, 110, 60 ];functionfinding_index(element) {returnelement > 25;}document.write(array.findIndex(finding_index));</script>chevron_rightfilter_none - Output:
2
9. find() Method: This method is used to get the value of the first element in the array that satisfies the provided condition. It checks all the elements of the array and whichever the first element satisfies the condition is going to print.
- Example:
<script>// Input array contain some elements.vararray = [10, 20, 30, 40, 50];// Function (return element > 10).varfound = array.find(function(element) {returnelement > 20;});// Printing desired values.document.write(found);</script>chevron_rightfilter_none - Output:
30
10. fill() Method: This method used to fill the array with a given static value. The value can be used to fill the entire array or it can be used to fill a part of the array.
- Example:
<script>// JavaScript code for fill() functionfunctionfunc() {vararr = [1, 23, 46, 58];// Here value = 87, start index = 1 and// and last index = 3arr.fill(87, 1, 3);document.write(arr);}func();</script>chevron_rightfilter_none - Output:
1, 87, 87, 58
11. forEach() Method: This method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
- Example:
<script>functionfunc() {// Original arrayconst items = [1, 29, 47];const copy = [];items.forEach(function(item){copy.push(item*item);});document.write(copy);}func();</script>chevron_rightfilter_none - Output:
1, 841, 2209
12. sort() Method: This method is used to sort the array. An array can be of any type i.e. string, numbers, characters etc.
- Example:
<script>// Original arrayvarnumbers = [88, 50, 25, 10];// Performing sort methodvarsub = numbers.sort(geeks);functiongeeks(a, b) {returna - b;}document.write(sub)</script>chevron_rightfilter_none - Output:
10, 25, 50, 88
13. concat() Method: This method is used to merge two or more arrays together. This function does not alter the original arrays passed as arguments.
- Example:
<script>// JavaScript code for concat() functionfunctionfunc() {varnum1 = [11, 12, 13],num2 = [14, 15, 16],num3 = [17, 18, 19];document.write(num1.concat(num2, num3));}func();</script>chevron_rightfilter_none - Output:
11, 12, 13, 14, 15, 16, 17, 18, 19
14. includes() Method: This method is used to know either a particular element is present in the array or not and accordingly, it returns true or false i.e, if the element is present, then it returns true otherwise false
- Example:
<script>// Taking input as an array A// having some elements.varA = [ 1, 2, 3, 4, 5 ];// Include() function is called to// test whether the searching element// is present in given array or not.a = A.includes(2)// Printing result of function.document.write(a);</script>chevron_rightfilter_none - Output:
true
15. reverse() Method: This method is used for in-place reversal of the array. The first element of the array becomes the last element and vice versa.
- Example:
<script>functionfunc() {//Original Arrayvararr = [34, 234, 567, 4];document.write("Original array: "+ arr);//Reversed arrayvarnew_arr = arr.reverse();document.write("<br>Newly reversed array: ");document.write(new_arr);}func();</script>chevron_rightfilter_none - Output:
Original array: 34, 234, 567, 4 Newly reversed array: 4, 567, 234, 34
Recommended Posts:
- JavaScript Basic Array Methods
- JavaScript | Array Iteration Methods
- What is the difference between every() and some() methods in JavaScript ?
- JavaScript | Get Date Methods
- How to get all the methods of an object using JavaScript ?
- JavaScript | Set Date Methods
- JavaScript | String Methods
- JavaScript | Object Methods
- What is the difference between find() and filter() methods in JavaScript ?
- JavaScript methods to increment/decrement alphabetical letters
- Array Helper Methods in ES6
- What is the difference between Object.keys() and Object.entries() methods in JavaScript ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to convert Integer array to String array using JavaScript ?
- What is the difference between Array.slice() and Array.splice() in JavaScript ?
- Difference between array.size() and array.length in JavaScript
- How to move an array element from one array position to another in JavaScript?
- How to check whether an array is subset of another array using JavaScript ?
- How to get the elements of one array which are not present in another array using JavaScript?
- What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


