JavaScript String lastIndexOf() method

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

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

Syntax 1:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string.lastIndexOf(char)
string.lastIndexOf(char)
string.lastIndexOf(char) 

Syntax 2:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string.lastIndexOf(ch,index)
string.lastIndexOf(ch,index)
string.lastIndexOf(ch,index) 

Syntax 3:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string.lastIndexOf(str)
string.lastIndexOf(str)
string.lastIndexOf(str)

Syntax 4:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
string.lastIndexOf(str,index)
string.lastIndexOf(str,index)
string.lastIndexOf(str,index)

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

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

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<script>
var a ="Best tutorial website: w3schools";
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>
<!DOCTYPE html> <html> <body> <script> var a ="Best tutorial website: w3schools"; 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>
<!DOCTYPE html>
<html>
<body>
<script>
var a ="Best tutorial website: w3schools";
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>