In JavaScript, String Object provides a function lastIndexOf that is most often used to get the last position of specified string pattern passed to the lastIndexOf function. JavaScript lastIndexOf function returns the index position for the last occurrence of specified string pattern in the provided string. By default lastIndexOf function starts searching from last index of the string i.e. equal to the length of the string. If you will pass the optional parameter of start index then it starts searching the string from the specified index.

Syntax for Javascript lastIndexOf function

Object.lastIndexOf(substring, startIndex);

Above syntax of javascript lastIndexOf function shows that the function accepts 2 types of parameters:

1. substring: substring parameter of lastIndexOf function accepts the string type value whose last index is to be searched from the given string expression.

2. startIndex: optional: startIndex parameter accepts the integer value to specify the start position for the lastIndexOf function. As javascript lastIndexOf function starts searching from the right side of the string, startIndex parameter value should be equal to or less than the length of the given string. It searched from the specified index upto first index of the string i.e. first character of the given string.

Example 1

var myString = new String();

myString = "Javascript lastIndexOf Function";

document.write(myString.lastIndexOf("i"));

Example 2

document.write(myString.lastIndexOf("i", myString.length));

Example 3

document.write(myString.lastIndexOf("i", myString.lastIndexOf("i") - 1));

Continue to next tutorial: JavaScript Substring to learn how to get a part of provided string by specifying the start index and end index to the substring function.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.