JavaScript String lastIndexOf() Method

You are Here:

JavaScript String lastIndexOf() Method

The lastIndexOf() method returns the position of the last occurrence of a specified value in a string.

Note: The string is searched backwards, starting at start index.

Note: This method is case sensitive.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var str = "the end will never end"; var result = str.lastIndexOf("end"); document.write(result); </script> </body> </html>

Syntax

str.lastIndexOf(searchString, startFrom)

Parameter Values

ValueTypeExplanation
searchStringRequiredSpecifies the characters to be searched for within this string.
startFromoptionallastIndexOf() by default reads the string from right to left.
Specifies the starting position of a string (str) from right to left.
If omitted, the default value is '0'.

Return Value

ValueCondition
-1When the searched value is not found within this string.
0 to str.lengthA index of a search value within this string.

lastIndexOf() with Specified Starting Position

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var str = "the end will never end"; var result = str.lastIndexOf("end", 10); // str = "the end will"; document.write(result); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author