String.prototype.lastIndexOf(searchString) returns the last index where a substring starts. If it is missing, you get -1. Need the first match? Use indexOf(). Need only yes/no? Prefer includes().
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.lastIndexOf. Start with this line.
JavaScript
str.lastIndexOf(searchString, position?)
Call
What it does
str.lastIndexOf(searchString, position?)
Primary signature.
string.lastIndexOf(...)
Call with the arguments your case needs.
Built into JavaScript. Call lastIndexOf(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
str.lastIndexOf(searchString, position?)
Parameters
Parameter
Description
searchString
Required. Substring to look for
position
Optional. Only consider matches with index ≤ this value
Return Value
Return Value
Description
*
Index (≥ 0) or -1
JavaScript Examples
Example 1 — Find the Last Index
Get the position of the last match, or -1 when it is missing.
JavaScript
const text = "I think Ruth's dog is cuter than your dog!";
console.log(text.lastIndexOf("dog")); // 38
console.log(text.lastIndexOf("cat")); // -1
console.log(text.lastIndexOf("I")); // 0 (valid index!)
Case-insensitive. After toLowerCase(), the last "brave" is still at 7.
Compatibility
Browser & Runtime Support
String.prototype.lastIndexOf() is a long-standing language feature. It works in every modern browser, Internet Explorer, and every current Node.js / Deno / Bun release.
✓ Baseline · Widely available
String.prototype.lastIndexOf()
Supported in all evergreen browsers, Internet Explorer (legacy), and every current JavaScript runtime.
100%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
String.lastIndexOf()Universal
Bottom line: Safe everywhere. Use indexOf() for the first match and includes() for clearer membership checks — lastIndexOf() when you specifically need the last index.
Frequently Asked Questions
lastIndexOf(searchString, position?) returns the zero-based index of the last occurrence of searchString at or before position. If nothing is found, it returns -1.
Yes. "Blue Whale, Killer Whale".lastIndexOf("blue") returns -1. Normalize with toLowerCase() when you need a case-insensitive search.
indexOf() finds the first match from the start (or from a start position forward). lastIndexOf() finds the last match at or before a from-position (default: end of the string).
Only matches whose start index is less than or equal to position are considered. If position is past the end, the whole string is searched. If position is less than 0, only index 0 is considered.
With no position it returns str.length. With a position inside the string it returns that position (clamped). Empty needles always “match” at a boundary.
No. Strings are immutable. lastIndexOf() only reads and returns a number.