Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

String.prototype.lastIndexOf()

BaselineWidely available

ThelastIndexOf() method ofString values searches this string and returns the index of the last occurrence of the specified substring. It takes an optional starting position and returns the last occurrence of the specified substring at an index less than or equal to the specified number.

Try it

const paragraph = "I think Ruth's dog is cuter than your dog!";const searchTerm = "dog";console.log(  `Index of the last ${searchTerm} is ${paragraph.lastIndexOf(searchTerm)}`,);// Expected output: "Index of the last "dog" is 38"

Syntax

js
lastIndexOf(searchString)lastIndexOf(searchString, position)

Parameters

searchString

Substring to search for. All values arecoerced to strings, so omitting it or passingundefined causeslastIndexOf() to search for the string"undefined", which is rarely what you want.

positionOptional

The method returns the index of the last occurrence of the specified substring at a position less than or equal toposition, which defaults toInfinity. Ifposition is greater than the length of the calling string, the method searches the entire string. Ifposition is less than0, the behavior is the same as for0 — that is, the method looks for the specified substring only at index0.

  • 'hello world hello'.lastIndexOf('world', 4) returns-1 — because, while the substringworld does occurs at index6, that position is not less than or equal to4.

  • 'hello world hello'.lastIndexOf('hello', 99) returns12 — because the last occurrence ofhello at a position less than or equal to99 is at position12.

  • 'hello world hello'.lastIndexOf('hello', 0) and'hello world hello'.lastIndexOf('hello', -5) both return0 — because both cause the method to only look forhello at index0.

Return value

The index of the last occurrence ofsearchString found, or-1 if not found.

Description

Strings are zero-indexed: The index of a string's first character is0, and the index of a string's last character is the length of the string minus 1.

js
"canal".lastIndexOf("a"); // returns 3"canal".lastIndexOf("a", 2); // returns 1"canal".lastIndexOf("a", 0); // returns -1"canal".lastIndexOf("x"); // returns -1"canal".lastIndexOf("c", -5); // returns 0"canal".lastIndexOf("c", 0); // returns 0"canal".lastIndexOf(""); // returns 5"canal".lastIndexOf("", 2); // returns 2

Case-sensitivity

ThelastIndexOf() method is case sensitive. For example, the followingexpression returns-1:

js
"Blue Whale, Killer Whale".lastIndexOf("blue"); // returns -1

Examples

Using indexOf() and lastIndexOf()

The following example usesindexOf() andlastIndexOf() to locate values in the string"Brave, Brave New World".

js
const anyString = "Brave, Brave New World";console.log(anyString.indexOf("Brave")); // 0console.log(anyString.lastIndexOf("Brave")); // 7

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-string.prototype.lastindexof

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp