• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Actual Wizard

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

JavaScript String lastIndexOf Method Explained

September 10, 2020 by Kevin Marszalek

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
}

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to Get the Last Element of an Array in JavaScript
    There are a number of different ways to get the last element of an array in JavaScript. Below we will explore …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2025 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy