JavaScript String lastIndexOf() method

The JavaScript string lastIndexOf() method retrieves the position of a char value present in the given string from last position.

Note: It is similar to indexOf(). Only difference is that lastIndexOf() returns the last index of match instead of first.

Syntax 1:

string.lastIndexOf(char) 

Syntax 2:

string.lastIndexOf(ch,index) 

Syntax 3:

string.lastIndexOf(str)

Syntax 4:

string.lastIndexOf(str,index)

Parameters:
char: It represents the specified char like “c”.
str: It represents the specified string like “w3spoint”.
index: It represents the specified position from where search have to start.

Return:
It will returns the position of a char value if char is present in the string otherwise returns -1.

Example:

<!DOCTYPE html>
<html>
<body>
<script>   
var a ="Best tutorial website: w3spoint";  
document.write(a.lastIndexOf('e'));  
document.write("</br>"); 
document.write(a.lastIndexOf('e', 2)); 
document.write("</br>"); 
document.write(a.lastIndexOf("es")); 
document.write("</br>"); 
document.write(a.lastIndexOf("es", 4)); 
</script>
</body>
</html>

Output:

26
1
26
1
Please Share