The lastIndexOf() method is useful for determining the index at which a string is contained in another string.
MyIndex = MyString.lastIndexOf("Test");
If the string that is passed as a parameter to the lastIndexOf() method does not exist in the string, then the method will return negative one.
This method is different from indexOf() because it will return the index of the last occurrence of the matching string, rather than the first.
The example below demonstrates a typical use case for the lastIndexOf() method.
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>JavaScript String lastIndexOf() Method Example</title>
</head>
<body>
<script>
var MyString = "Hello World! Hello Universe!";
var TestString = "Hello";
var TestResult = MyString.lastIndexOf(TestString);
if (TestResult >= 0){
console.log("The String: \'" + MyString + "\' contains:\'" + TestString + "\' at position: " + TestResult);
}else{
console.log("The String: \'" + MyString + "\' does not contain:\'" + TestString + "\'");
}
</script>
</body>
</html>
The following is printed to the console:
The String: 'Hello World! Hello Universe!' contains:'Hello' at position: 13
Using the Optional Start Position Value Parameter
You can optionally include a start position value with the lastIndexOf() method.
It is important to know that the start position is an index value that starts at zero.
This can be confusing, as demonstrated below:
var MyString = "ABCDCDC";
var TestString = "C";
var StartPosition = 4;
//TestString[4] == C;
TestResult = MyString.lastIndexOf(TestString,StartPosition);
if (TestResult >= 0){
console.log("The String: \'" + MyString + "\' contains:\'" + TestString + "\' at the position of : " + TestResult);
}else{
console.log("The String: \'" + MyString + "\' does not contain:\'" + TestString + "\' at the start position of : " + StartPosition);
}
The following is printed to the console:
The String: 'ABCDCDC' contains:'C' at the position of : 4
Tip: Using a value for the start position that is larger than the length of the string does not generate a warning or an error, but the method will always return the value negative one.
Passing a Null Value As a Parameter to the includes() Method
This method will behave in a quirky and undesirable way if you accidentally pass an empty string as a parameter.
In the line of code below, the value of x will always be equal to 0, no matter what the value of MyString is.
x = MyString.lastIndexOf("");
includes() Method Vs lastIndexOf() Method
These two methods are similar, and both can be used to determine if a specific string contains another string, but there are some differences.
The includes() method returns a boolean value compared to the lastIndexOf() method, which returns a number.
The following code below is identical in functionality, although using lastIndexOf() is less efficient as the index value is not used in the example.
var MyString = "The Sky is Blue.";
var TestString = "Sky";
//three different ways to do the same test
MyString = "The Sky is Blue.";
TestString = "Sky";
if (MyString.includes(TestString)) {
//do something
}
//same function as above
if (MyString.lastIndexOf(TestString) >= 0) {
//do something
}
//this code using indexOf() is also functionally identical
if (MyString.indexOf(TestString) >= 0) {
//do something
}