Skip to content
Related Articles

Related Articles

Improve Article

JavaScript String lastIndexOf() Method

  • Last Updated : 19 Oct, 2020

Below is the example of the String lastIndexOf() Method.

  • Example:




    <script> 
    function func() { 
        var str = 'GeeksforGeeks'
        var index = str.lastIndexOf('for'); 
        document.write(index); 
    func(); 
    </script>
  • Output:
    5
    

str.lastIndexOf() method 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)
    Perameters:

  • searchValue: searchValue is the string that is to be searched in the base string.
  • index defines the starting index from where the searchValue is to be searched in the base string backwards.

Return value: This method 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 method returns -1.



Examples for the above method are provided below:

Example 1:

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

Output:

9

In this example the method lastIndexOf() finds the last index of the string ‘Train’ in the string str. Since the only index of the string is 9, therefore this method 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 method lastIndexOf() 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 method returns 6 as the answer.

Example 3:



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

Output:

-1

In this example the method lastIndexOf() 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 method returns -1 as the answer.

Example 4:

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

Output:

6

In this example the method lastIndexOf() 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 method returns 30 as the answer.

Codes for the above method are provided below:

Program 1:




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

Output:

9

Program 2:




<script>
// JavaScript to illustrate lastIndexOf() method
  
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> 

Output:

6

Program 3:




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

Output:

-1

Program 4:




<script>
// JavaScript to illustrate lastIndexOf() method
  
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>

Output:

30



My Personal Notes arrow_drop_up
Recommended Articles
Page :