How to get the last index of an occurrence of the specified value in a string in JavaScript?


In this tutorial, we will learn how to get the last index of an occurrence of the specified value in a string in JavaScript.

The last index of an occurrence means the last position found of the searched item in a given string. It can be a single character or a word also. To find the index of the last occurrence, we have a method in JavaScript that is the lastIndexOf() method.

Using the lastIndexOf() Method

This method will search the whole string and return the index of that value's last position. If it doesn’t find the item, it will return -1.

lastIndexOf() method is case sensitive here, “A” and “a” is recognized as different value. The string index position will start from 0, and the end index will be the length of string-1.

Syntax

Users can follow the syntax below to get the last index of an occurrence of the specified value in a string in JavaScript.

string.lastIndexOf(searchItem,start);

In the above syntax, users can see that there are two parameters for the lastIndexOf() method first one is the string value that is searched. It is a required one and the second parameter is optional where we give the position (default value is string length).

Example

In the below example, we create a string variable “str” to store the string value. We search for the last index of occurrence of the value "Learn". To find the index we use lastIndexOf() method. The original string is "Learn for free and enjoy! Learn and share!".

<html> <head> <title>JavaScript String lastIndexOf() Method</title> </head> <body> <h3> Getting the last index of an occurrence in an string</h3> <div id = "result1"> String:</div> <div id = "result2"> Search Value:</div> <div id = "result3"> Index:</div> <script> var str1 = new String("Learn for free and enjoy! Learn and share!"); document.getElementById("result1").innerHTML += str1; document.getElementById("result2").innerHTML += "Learn"; var index = str1.lastIndexOf("Learn"); document.getElementById("result3").innerHTML += index; </script> </body> </html>

In the above example, we search for the last index of occurrence of "Learn" in the string "Learn for free and enjoy! Learn and share!". Notice that the string has two occurrences of Learn. You can try to find the last index of an occurrence with signle character.

Example

In the below example, we used a button, “Click Here” with the onclick function last_index(). Inside the function, a string variable “str” and a search variable take the input from the user using the prompt method. Then the lastIndexOf() method takes the search item as a parameter and is applied to the string “str”. Then return the index to the “index_element” variable.

<html> <body> <h3>Getting the last occurrence of a value in string from user input.</h3> <p>Click the below button to take user inputs</p> <button onclick = "last_index()"> Click Here</button> <p id = "result"></p> <script> function last_index() { let str = prompt("Please enter the string", "all for one and one for all"); let search = prompt("Enter the search value", "all"); let index_element = str.lastIndexOf(search); document.getElementById("result").innerHTML = "The last occurence of "" + search + "" in "" + str + "" is " + index_element; } </script> </body> </html>

As users can see, after clicking the “Click Here” button, the last_index() function is invoked, and it shows an alert message to enter the string; then, after clicking ok, the second alert message shows to enter the search value. After inserting the values, we will get the index position of the search value in the string.

In this tutorial, we discussed the lastIndexOf() method to find the last index of an occurrence of a specified value in a string. This method is straightforward to find the index.

Updated on: 20-Oct-2022

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements