Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptJavaScript String lastIndexOf() Method

JavaScript String lastIndexOf() Method

JavaScript String 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)

Parameters:

  • searchvalue: The 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 backward

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

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

Example 1: 

JavaScript




function func() {
    let str = 'neveropen';
    let index = str.lastIndexOf('for');
    console.log(index);
}
func();


Output:

5

Example 2: 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.

JavaScript




// JavaScript to illustrate lastIndexOf() method
 
function func() {
 
    //Original string
    let str = 'Departed Train';
 
    //Finding the index of the string
    let index = str.lastIndexOf('Train');
    console.log(index);
}
func();


Output: 

9

Example 3: 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. 

JavaScript




// JavaScript to illustrate lastIndexOf() method
 
function func() {
 
    // Original string
    let str = 'Departed Train';
 
    // Finding the index of the string
    let index = str.lastIndexOf('ed Tr');
    console.log(index);
}
func();


Output:

6

Example 4: 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. 

JavaScript




// JavaScript to illustrate lastIndexOf() method
 
function func() {
 
    // Original string
    let str = 'Departed Train';
 
    // Finding index of occurrence of 'train'
    let index = str.lastIndexOf('train');
    console.log(index);
}
func();


Output: 

-1

Example 5: In this example, the method lastIndexOf() finds the last index of the string Train in the string str. Since the string is present two times in the string str and the index 30, is the last index of Train, therefore this method returns 30 as the answer. 

JavaScript




// JavaScript to illustrate lastIndexOf() method
 
function func() {
 
    // Original string
    let str = 'Departed Train before another Train';
 
    // Finding index of occurrence of 'Train'
    let index = str.lastIndexOf('Train');
    console.log(index);;
}
func();


Output:

30

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported Browsers: 

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 6 and above
  • Opera 3 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.

RELATED ARTICLES

Most Popular

Recent Comments