JavaScript Array lastIndexOf() Method

Below is the example of the Array lastIndexOf() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        var name = [ 'gfg', 'cse', 'geeks', 'portal' ];
      
        a = name.lastIndexOf('gfg')
      
        // Printing result of method
        document.write(a);
    </script>

    chevron_right

    
    

  • Output:
    0

The arr.lastIndexOf() method is used to find the index of the last occurrence of the search element provided as the argument to the function.

Syntax:

array.lastIndexOf(element, start)

Paramaters: This method accepts two parameters as mentioned above and described below:

  • element: This parameter holds the element which index will be return.
  • start: This parameter is optional and it holds the starting point of the array, where to begin the search the default value is 0.

Return value: This method returns the index of the first occurrence of the element. If the element cannot be found in the array, then this method returns -1.



Below examples illustrate the Array lastIndexOf() method in JavaScript:

  • Example 1: In this example the method will searched for the element 2 in that array return that element indesx.
    Input : [1, 2, 3, 4, 5].lastIndexOf(2);
    Output: 1
  • Example 2: In this example the method will searched for the element 9 in that array if not found then return -1.
    Input : [1, 2, 3, 4, 5].lastIndexOf(9);
    Output: -1

Code for the above method is provided below:

Program 1:

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 ];
  
    // lastIndexOf() method is called to
    // test whether the searching element
    // is present in given array or not.
    a = A.lastIndexOf(2)
  
    // Printing result of method.
    document.write(a);
</script>

chevron_right


Output:

1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // Taking input as an array A
    // having some elements.
    var name = [ 'gfg', 'cse', 'geeks', 'portal' ];
  
    // lastIndexOf() method is called to
    // test whether the searching element
    // is present in given array or not.
    a = name.lastIndexOf('cat')
  
    // Printing result of method
    document.write(a);
</script>

chevron_right


Output:

-1

Supported Browsers: The browsers supported by JavaScript Array lastIndexOf() method are listed below:

  • Google Chrome
  • Microsoft Edge 9.0
  • Mozilla Firefox 1.5
  • Safari
  • Opera

full-stack-img




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.