JavaScript String lastIndexOf()

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

The lastIndexOf method in javascript returns the index of the last occurrence of the specified substring. Given a second argument, a number, the lastIndexOf() method returns the last occurrence of the specified substring at an index less than or equal to the number.
The lastIndexOf() method returns -1 if the argument string is not found in the base string.

The String.lastIndexOf() method in JavaScript searches the string from the end to the beginning of the string. If this method founds an occurrence of the argument provided, the lastIndexOf() method stops the searching and returns the 0-based index of the argument string. If the argument string is not present, then this method returns -1.

But note that, the lastIndexOf() in javascript returns the index starting from the beginning of the string.

What is lastIndexOf in javascript

As you can see in the above image. String "JS" is being searched in the string "JS IS A SCRIPTING LANGUAGE".

Case-sensitivity

The lastIndexOf() in javascript is case-sensitive. Therefore "javascript" and "JavaScript" are two different strings for this method.

Syntax of String lastIndexOf() in Javascript

The following is the syntax of the lastIndexOf() in Javascript:

Parameters of String lastIndexOf() in Javascript

  • stringToSearch The string to be searched in the base string is stringToSearch.
    For example, if we have to find the last occurrence of "JS" in the string "JS IS A SCRIPTING LANGUAGE". Here "JS" is the stringToSearch and the base string is "JS IS A SCRIPTING LANGUAGE".

  • position The lastindexof in javascript returns the index of the last occurrence of the argument string, here stringToSearch, at an index less than or equal to the position. By default position's value is infinity.

Return Value of String lastIndexOf() in Javascript

Return type: number

The lastindexof in javascript returns the 0-based index of the last occurrence of stringToSearch or returns -1 if the index is not found.

Example of String lastIndexOf() Method in JavaScript

Let's have an example to understand our learnings.

Output:

The lastindexof in javascript is case-sensitive. Therefore "javascript" and "JavaScript" are two different strings for this method. Let's have an example for this case:

Output:

This time the lastIndexOf() method returned -1, as this method treats "javascript" and "JavaScript" differently.

Using indexOf() and lastIndexOf()

Suppose we have to find the first and last index of a string in another base string. We will use the indexOf() method and the lastIndexOf() method to obtain the respective indexes.

Output:

A String is a 0-based indexed data type, this means that the first character of a string is present at index 0.

For example: Consider the string "APPLE". The first character of this string that is "a" is present at index 0.

Now, let's have a base string "THIS IS A BASE STRING".

last position of the first character of string

So, now we have to return the last index of character "A". In the above image, we can see the last index of character "A" is 11. Therefore we will return 11.
To find the last position of the first character of a string, we can use the lastIndexOf() method. Instead of passing the stringToSearch, we will pass the first character of stringToSearch, which is stringToSearch[0]. Let's look over the example to understand this:

Output:

The last position of the character "a" in the base string is 11, therefore lastIndexOf() method returns 11.

Conclusion

  • The String.lastIndexOf() method in JavaScript returns the last index of the argument string in the base string.
  • If the argument string is not found in the base string then, lastIndexOf() method returns -1.
  • The lastIndexOf() method has two parameters, stringToSearch and position (optional).
  • The indexOf() method and lastIndexOf() method is used to find the first and the last occurrence of a string in a base string.