Tutorialspoint.dev

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:

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

Output:

9

Program 2:

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

Output:

6

Program 3:

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

Output:

-1

Program 4:

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

Output:

30


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter