JavaScript

JavaScript String lastIndexOf() Method

The String lastIndexOf() method is a built-in searching function of JavaScript. It is used to explore the index of a particular string or character in a given sequence of characters. It looks like the indexof() method. However, it starts searching for elements from the last position of the given string. In the specified string, the index position starts with 0. It returns -1 if there is no element present. JavaScript String lastIndex() method is a case sensitive function. All the latest browsers support this function as it belongs to ECMAScript1 features. This guide graps a piece of descriptive knowledge of the following outcomes.

How does the String lastIndexOf() method work in JavaScript

The JavaScript String lastIndexOf() method works in backward order as it explores the string from the end to the beginning position..

Let’s have a look at the syntax of the string lastIndexOf() method.

string.lastIndexOf(searchvalue, start)

Here, the ‘searchvalue’ is a substring that is used to find for within a string. The ‘start’ is a parameter that represents the index from where the searching will start.

The String lastIndexOf() method retrieves the index of the last value in a string.

How to use the String lastIndexOf() method in JavaScript

The lastIndexOf() method starts searching from end to beginning pattern. This section explains the usage of the lastIndexOf() method  with the help of examples.

Example: How to find the index of a specific character in a string
The string lastindexOf() method helps in finding a particular character in the sequence of characters. In the following example, we will show you how it works.

string = "LinuxHint helps in programming";
output = string.lastIndexOf('H');
console.log(output);

In this code, a string “LinuxHint helps in programming” is initialised. ‘H’ is a particular character in the string that is to be searched. When the function calls it finds and returns the index position of the specified character.

The returned output showed that the specified character ‘H’ is located at the index position ‘5’ in the string.

Example: How String lastIndexOf() method behaves when a substring is not found
The lasIndexOf() method retrieves the index position of a specified character in a given string. This example explains how this function works with the null string.

string = "LinuxHint helps in programming";
output = string.lastIndexOf('Talha Saif Inc');
console.log(output);

In this example, a string “LinuxHint helps in programming” is passed to an object. We use the substring “Talha Saif Inc” that is to be searched. When a function calls, it returns the location of the specified substring.

The returned output showed that the specified substring is not located in the given string. Therefore the function returned -1.

Example: How the String lastIndexOf() method works with defined start parameter
The lastIndexOf() method works in the backward order. However if the start parameter is defined, it will work in a different scenario. Let’s have a look at how it works.

string = "LinuxHint is a Linux-based content provider";
output = string.lastIndexOf('n', 4);
console.log(output);

In this example, the starting index of the given string is defined with the index position ‘4’. When the function calls, it will not search the specified index throughout the whole string. But it will only search in the defined index point.

The returned output showed that the specified character ‘n’ is present at the index ‘2’ with a defined start parameter.

Example: How the String lastIndexOf() method reacts in a case insensitive matching
The lastIndexOf() method has a case insensitive property. This example checks how the String lastIndexOf() method reacts in a case-insensitive matching.

string = "LinuxHint helps in programming";
output = string.lastIndexOf('P');
console.log(output);

In this example, a string “LinuxHint helps in programming” is passed to an object.  However, there is a case sensitivity in the substring ‘P’.

The output showed that in the case-insensitivity scenario, the function returned the ‘-1’ output.

Conclusion

JavaScript String lastIndexOf() method is used to find the indexof a particular string or character in a given sequence of characters. This short guide provides you with a complete guide about the String lastIndexOf() method. Furthermore, we also illustrated the working of the lastIndexOf() method with its syntax and usage in JavaScript along with suitable examples.

About the author

Adnan Shabbir