JavaScript | Array lastIndexOf()

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

arr.lastIndexOf(searchElement[,index])

Arguments The first argument to this function is the searchElement which is the value to be searched in the array. Second argument to this function is the optional index argument which defines the starting index in the array from where the element is to be searched backwards. If this argument is not provided then index arr.length – is taken as the starting index to begin the backward search as it is the default value.

Return value This function returns the index of the last occurrence of the searchElement. If the element cannot be found in the array, then this function returns -1



Examples for the above function are provided below:

Example 1:

var array = [2, 9, 9];
print(array.lastIndexOf(2));

Output:

0

In this example the function lastIndexOf() returns the last index of occurrence of 2 which is 0

Example 2:

var array = [2, 98, 12, 45];
print(array.lastIndexOf(45,2));

Output:

-1

In this example the function lastIndexOf() finds the last index of occurrence of 45. Since it is not present starting from index 2, therefore the function returns -1.
Example 3:

var array = [2, 98, 12, 45];
print(array.lastIndexOf(98,2));

Output:

1

In this example the function lastIndexOF() returns the last occurrence of 98 as 1.

Example 4:


var array = [2, 98, 12, 45];
print(array.lastIndexOf(100));

Output:

-1

In this example the function lastIndexOF() checks the last occurrence of 100. Since it is not present therefore it returns -1

Example 5:

var array = [2, 98, 12, 98];
print(array.lastIndexOf(98));

Output:

3

Codes for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() 
  
function func() {
  
    // Original array
    var array = [2, 9, 9];
    document.write(array.lastIndexOf(2));
}
  
func();
</script>

chevron_right


Output:

0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() 
  
function func() {
  
    //Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(45,2));
}
  
func();
</script>

chevron_right


Output:

-1

Program 3:


filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    // Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(98,2));
}
  
func();
</script>

chevron_right


Output:

1

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    // Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(100));
}
func();
</script>

chevron_right


Output:

-1

Program 5:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    //Original array
    var array = [2, 98, 12, 98];
    document.write(array.lastIndexOf(98));
}
func();
</script>

chevron_right


Output:

3


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.



Improved By : ManasChhabra2



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.