Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String lastIndexOf() Method

Improve Article
Save Article
  • Last Updated : 01 Dec, 2022
Improve Article
Save Article

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




<script>
    function func() {
        var str = 'GeeksforGeeks';
        var index = str.lastIndexOf('for');
        console.log(index);
    }
    func();
</script>

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




<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');
        console.log(index);
    }
    func();
</script>

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




<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');
        console.log(index);
    }
    func();
</script>

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




<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');
        console.log(index);
    }
    func();
</script>

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




<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');
        console.log(index); ;
    }
    func();
</script>

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!