JavaScript | String lastIndexOf()

str.lastIndexOf() function finds the index of the last occurrence of the argument string in the given string. The value returned is 0-based.

Syntax:
str.lastIndexOf(searchValue , index)

Arguments the first argument to the function searchValue is the string that is to be searched in the base string. The second argument to the function index defines the starting index from where the searchValue is to be searched in the base string backwards.

Return value This function returns the index of the string (0-based) where the searchValue is found for the last time. If the searchValue cannot be found in the string then the function returns -1.

Examples for the above function are provided below:

Example 1:

var str = 'Departed Train';
var index = str.lastIndexOf('Train');
print(index); 

Output:

9

In this example the function lastIndexOf() function finds the last index of the string ‘Train’ in the string str. Since the only index of the string is 9, therefore this function returns 9 as the answer.

Example 2:

var str = 'Departed Train';
var index = str.lastIndexOf('ed Tr');
print(index); 

Output:

6

In this example the function lastIndexOf() function finds the last index of the string ‘ed Tr’ in the string str. Since the only index of where this string is present is 6, therefore this function returns 6 as the answer.

Example 3:

print('Departed Train'.lastIndexOf('train')); 

Output:

6

In this example the function lastIndexOf() function finds the last index of the string ‘train’ in the string str. Since the string is not present in any index in str, therefore this function returns -1 as the answer.

Example 4:

print('Departed Train before another Train'.lastIndexOf('Train')); ; 

Output:

6

In this example the function lastIndexOf() function finds the last index of the string Train in the string str. Since the string is present two time in the string str and the index 30, is the last index of Train, therefore this function returns 30 as the answer.

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
  
function func() {
      
    //Original string
    var str = 'Departed Train';
      
    //Finding the index of the string
    var index = str.lastIndexOf('Train');
    document.write(index);
}
func();
</script>

chevron_right


Output:

9

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
      
    // Original string
    var str = 'Departed Train';
      
    // Finding the index of the string
    var index = str.lastIndexOf('ed Tr');
    document.write(index); 
}
func();
</script> 

chevron_right


Output:

6

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
     
    // Original string
    var str = 'Departed Train';
      
    // Finding index of occurrence of 'train'
    var index = str.lastIndexOf('train');
    document.write(index);
}
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 string
    var str = 'Departed Train before another Train';
      
    // Finding index of occurrence of 'Train'
    var index = str.lastIndexOf('Train');
    document.write(index); ;
}
func();
</script>

chevron_right


Output:

30


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.




Article Tags :

Be the First to upvote.


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