JavaScript String lastIndexOf() Method - GeeksforGeeks

05 Oct, 2021
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>
<script> function func() {     var str = 'GeeksforGeeks';     var index = str.lastIndexOf('for');     document.write(index); } func(); </script>
Output:5

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)

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

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
makinenidharmendra
ysachin2314
JavaScript-Methods
javascript-string
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?