JavaScript lastIndexOf() method


The lastIndexOf() method in JavaScript is used to determine the index of the last occurrence of a specified substring (search value) within a string. If the substring is not found, it returns -1. This method is case-sensitive and can also accept an optional starting position from which to begin the search, searching backwards through the string.

Syntax:

string.lastIndexOf(searchValue, fromIndex)
  • searchValue: The substring you want to search for within the string.
  • fromIndex (optional): An integer that specifies the index at which to start the search. The search will begin from this index and go backwards. If omitted, the search starts from the end of the string.

Return Value:

  • It returns the zero-based index of the last occurrence of searchValue within the string. If the substring is not found, it returns -1.

Example 1: Basic Usage

let text = "Hello, world! Welcome to the world!"; let lastIndex = text.lastIndexOf("world"); console.log(lastIndex); // 27

In this example, the substring "world" is found at index 27, which is the last occurrence in the string.

Example 2: Case Sensitivity

The lastIndexOf() method is case-sensitive. This means that searching for "World" will not match "world".

let message = "Hello, World! Hello, world!"; console.log(message.lastIndexOf("world")); // 26 console.log(message.lastIndexOf("World")); // 7 (case-sensitive, found at a different index)

Example 3: Using fromIndex

You can specify a starting position for the search using the fromIndex parameter. The search will begin at this index and go backwards through the string.

let sentence = "The quick brown fox jumps over the lazy dog."; console.log(sentence.lastIndexOf("o")); // 26 (last occurrence) console.log(sentence.lastIndexOf("o", 25)); // 12 (the search ends at index 25, finding the first occurrence) console.log(sentence.lastIndexOf("o", 11)); // -1 (no occurrences before index 11)

Example 4: Not Finding the Substring

If the specified substring is not found within the string, lastIndexOf() will return -1.

let str = "Hello, world!"; console.log(str.lastIndexOf("example")); // -1

Example 5: Finding Empty Strings

When searching for an empty string (""), lastIndexOf() will return the index of the end of the string. This is because an empty string is considered to be at every index between characters, including the index after the last character.

let example = "Hello"; console.log(example.lastIndexOf("")); // 5 (the length of the string)

Example 6: Finding the First Occurrence

If you want to find the first occurrence of a substring, you should use the indexOf() method.

let text = "Hello, world! Welcome to the world!"; console.log(text.indexOf("world")); // 7 (first occurrence) console.log(text.lastIndexOf("world")); // 27 (last occurrence)

Summary:

  • The lastIndexOf() method finds the index of the last occurrence of a specified substring within a string and returns -1 if not found.
  • It is case-sensitive and can take an optional fromIndex argument to specify where to start searching backwards.
  • Searching for an empty string returns the index equal to the length of the string since an empty substring is considered to be at every position.
  • For finding the first occurrence of a substring, use the indexOf() method instead.