JavaScript String.lastIndexOf() Method

MD Niaz Rahman Khan Jan 30, 2023
  1. Syntax of JavaScript string.lastIndexOf():
  2. Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given Character From a Single String
  3. Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given Word From Multiple Strings
  4. Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given String With Case-Sensitivity
JavaScript String.lastIndexOf() Method

The string.lastIndexOf() method is used to find the last event of a string value. It shows the resulting index position (0-based) of a string.

This method is almost the same as the string.indexOf() method. The only difference between them is that the indexOf() method finds the first event, whereas the lastIndexOf() method finds the last event of a particular string.

Syntax of JavaScript string.lastIndexOf():

string.lastIndexOf(searchValue, indexPosition)

Parameters

searchValue (required) A specific string you want to search for.
indexPosition (optional) A specific index position that will identify the endpoint. By default, it is infinity.

Return

The lastIndexOf() method will return the last value’s index position (0-based) from a given string that is searched for if the value exists. Otherwise, it will return -1.

Note: The string.lastIndexOf() method is case-sensitive. That means it will not match if the actual string is in uppercase form and you searched for it in lowercase.

Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given Character From a Single String

let string = 'Welcome';
let lastIndex = string.lastIndexOf('e');
let lastIndexWithPosition = string.lastIndexOf('e', 2);
let noCharExists = string.lastIndexOf('a')

console.log(lastIndex)
console.log(lastIndexWithPosition)
console.log(noCharExists)

Output:

6
1
-1

We have taken a string named Welcome to perform the lastIndexOf() method.

First, we searched with the required parameter, the search value. We then searched for the character e and tried to find its last index position.

In our string, the character e exists two times. One is at index position 1, and the other is at index position 6.

This method only shows 6 because it is the last event of the character e.

Second, we have searched with both the required and the optional parameters, which are search value and index position. We have searched for the same value, e, but this time, we have set the boundary at index position 2.

As a result, it returns the index position 1 because this time, it is the last event of the character e.

Third, we have searched for a value named a that does not exist on the string, and as expected, it returns -1, which represents that the value was not found.

Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given Word From Multiple Strings

let string = 'The more you read, The more you learn';
let lastIndex = string.lastIndexOf('you');
let lastIndexWithPosition = string.lastIndexOf('you', 14);
let noWordExists = string.lastIndexOf('Great')

console.log(lastIndex)
console.log(lastIndexWithPosition)
console.log(noWordExists)

Output:

28
9
-1

First, we searched for the word you and tried to find out its last index position. The string.lastIndexOf() method will look into the string and returns the index position of the last occurrence of the first character of the search value.

In our case, the last occurrence of the string you was found at index position 28.

Second, we have searched with both the required and the optional parameters, which are search value and index position. We have searched for the same value, you, but this time, we have set the boundary at index position 1`.

As a result, it returns the index position 9 because this time, it is the last occurrence of the value you.

Third, we have searched for a value named Great that does not exist on the string, and as expected, it returns -1, which represents that the value was not found.

Example Codes: Use the string.lastIndexOf() Method to Find the Last Index Position of a Given String With Case-Sensitivity

let string1 = 'WelcomE'
let string2 = 'The more you read, The more You learn';

let caseSensitive1 = string1.lastIndexOf('e')
let caseSensitive2 = string2.lastIndexOf('you')

console.log(caseSensitive1)
console.log(caseSensitive2)

Output:

1
9

The string.lastIndexOf() method is case-sensitive. It will not be able to match the upper case value with the lower case.

First, we have taken a similar example but tweaked it a little. The first character of e is in lowercase, and the second character of E is in uppercase.

As a result, it shows the index position of the last occurrence for this value 1, which was 6 for the previous example. This is happening because of case sensitivity.

A similar case happened for the second example where the index position of the last occurrence for you was 28, which shows 9 now.

MD Niaz Rahman Khan avatar MD Niaz Rahman Khan avatar

Niaz is a professional full-stack developer as well as a thinker, problem-solver, and writer. He loves to share his experience with his writings.

LinkedIn

Related Article - JavaScript String