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:
  • filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

  • Output:
    true

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Original array
    var numbers = [88, 50, 25, 10];
       
    // Performing reduce method
    var sub = numbers.reduce(geeks);
       
    function geeks(total, num) {
        return total - num;
    }
       
    document.write(sub)
    </script>

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Original array
    var numbers = [4, 9, 16, 25];
       
    // Performing map method
    var sub = numbers.map(geeks);
       
    function geeks() {
        return numbers.map(Math.sqrt);
    }
       
    document.write(sub)
    </script>

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    // JavaScript code for every() function 
    function ispositive(element, index, array) { 
        return element > 0; 
       
    function func() { 
          
        var arr = [ 11, 89, 23, 7, 98 ]; 
           
        // Check for positive number 
        var value = arr.every(ispositive); 
          
        document.write(value); 
      
    func(); 
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    //Original array
    var arr = [ [11, 89], [23, 7], 98 ]; 
          
    // Performing flat method
    var geeks = arr.flat();
      
    document.write(geeks)
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    const myAwesomeArray = [[1], [2], [3], [4], [5]]
      
    var geeks = myAwesomeArray.flatMap(arr => arr * 10)
    console.log(geeks);
      
    </script>                    

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
       
    function isPositive(value) { 
        return value > 0; 
       
    function func() { 
        var filtered = [112, 52, 0, -1, 944]
        .filter(isPositive); 
        document.write(filtered); 
      
    func(); 
    </script>

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    var array = [ 10, 20, 30, 110, 60 ]; 
      
    function finding_index(element) { 
        return element > 25;
       
    document.write(array.findIndex(finding_index)); 
    </script>

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    // Input array contain some elements. 
    var array = [10, 20, 30, 40, 50]; 
      
    // Function (return element > 10). 
    var found = array.find(function(element) { 
        return element > 20; 
    }); 
      
    // Printing desired values. 
    document.write(found); 
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    // JavaScript code for fill() function 
    function func() {
      
        var arr = [1, 23, 46, 58]; 
          
        // Here value = 87, start index = 1 and 
        // and last index = 3 
        arr.fill(87, 1, 3); 
        document.write(arr); 
      
    func(); 
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
    function func() { 
      
        // Original array 
        const items = [1, 29, 47]; 
        const copy = []; 
      
        items.forEach(function(item){ 
            copy.push(item*item); 
        }); 
      
        document.write(copy); 
      
    func(); 
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // Original array
    var numbers = [88, 50, 25, 10];
      
    // Performing sort method
    var sub = numbers.sort(geeks);
      
    function geeks(a, b) {
        return a - b;
    }
      
    document.write(sub)
    </script>                    

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
    // JavaScript code for concat() function 
    function func() { 
        var num1 = [11, 12, 13], 
            num2 = [14, 15, 16], 
            num3 = [17, 18, 19]; 
      
        document.write(num1.concat(num2, num3)); 
    func(); 
    </script> 

    chevron_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
        // Taking input as an array A 
        // having some elements. 
        var A = [ 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_right

    
    

  • 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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script> 
      
    function func() { 
      
        //Original Array 
        var arr = [34, 234, 567, 4]; 
        document.write("Original array: " + arr); 
      
        //Reversed array 
        var new_arr = arr.reverse(); 
        document.write("<br>Newly reversed array: "); 
        document.write(new_arr); 
    func(); 
    </script> 

    chevron_right

    
    

  • Output:
    Original array: 34, 234, 567, 4
    Newly reversed array: 4, 567, 234, 34



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.