JavaScript String lastIndexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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?)
CallWhat 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

ParameterDescription
searchStringRequired. Substring to look for
positionOptional. Only consider matches with index ≤ this value

Return Value

Return ValueDescription
*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!)
Try It Yourself

How It Works

  1. Last match. "dog" appears twice; the later one starts at 38.
  2. Missing substring. "cat" is absent → -1.
  3. Index 0 is real. "I" is at the start → 0.
  4. Check with !== -1. Never treat the result as a plain boolean.

Example 2 — Limit the Search with position

Only matches that start at or before position are considered.

JavaScript
const text = "canal";

console.log(text.lastIndexOf("a"));     // 3
console.log(text.lastIndexOf("a", 2));  // 1
console.log(text.lastIndexOf("a", 0));  // -1
Try It Yourself

How It Works

  1. Default search. Last "a" in "canal" is at index 3.
  2. position = 2. The "a" at 3 is past the limit; the earlier one at 1 wins.
  3. position = 0. Only index 0 ("c") is allowed → -1.
  4. Think “at or before”. position caps how far right a match may start.

Example 3 — lastIndexOf vs indexOf

Same substring, different direction — first vs last index. Case still matters.

JavaScript
const text = "Brave, Brave New World";

console.log(text.indexOf("Brave"));      // 0
console.log(text.lastIndexOf("Brave"));  // 7

console.log(text.lastIndexOf("brave"));  // -1  (case differs)
console.log(text.toLowerCase().lastIndexOf("brave")); // 7
Try It Yourself

How It Works

  1. indexOf. Left → right; first "Brave" at 0.
  2. lastIndexOf. Finds the later "Brave" at 7.
  3. Case matters. "brave" does not match "Brave" → -1.
  4. Case-insensitive. After toLowerCase(), the last "brave" is still at 7.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge Supported · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

Next: startsWith()

Check whether a string begins with a given prefix — clearer than comparing indexOf to 0.

startsWith() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful