Updated October 13, 2023
Introduction to JavaScript String lastIndexOf()
Developers use the lastIndexOf() function in JavaScript for various purposes. The lastIndexOf() function can used on string and array for different tasks. Developers use the lastIndexOf() function in JavaScript to obtain the starting position of a particular word or character in a given string. The lastIndexOf() function is used to return the index position of a particular element in the given array. The lastIndexOf() function is a built-in function in JavaScript. This function is the same as the indexOf() function in JavaScript, but the difference is it starts finding an element from the last position of the string or an array.
The lastIndexOf() function returns the index position and always starts with zero of the first element in a string or an array. If the function fails to find an element in the string or array, then it will return.
Syntax of JavaScript String lastIndexOf()
There are different syntax to use the string lastIndexOf() depending on the tasks and parameters passed, as given below:
1. The Syntax of lastIndexOf() function for string:
- lastIndexOf(character): This function returns the last index position of a character passed as an argument within a string or array.
- lastIndexOf(character, indexpos): This function gives the last index position of a character but starts finding an element from the given index position in the inverse order.
- lastIndexOf(string): This function gives the last index position of the first character of a given string in the function.
- lastIndexOf(string, indexpos): This function gives the last index position of the first character of a given string but starts finding an element from the given index position in the inverse order.
2. The Syntax of lastIndexOf() function for array:
- lastIndexOf(element): This function gives the last index position of an element passed in the function.
- lastIndexOf(element, indexpos): This function gives the last index position of an element passed in the function but starts finding an element from the given index position in the inverse order.
Parameters of JavaScript lastIndexOf()
Below are the Parameters of JavaScript lastIndexOf():
- character: This parameter specify a single character whose index position is to be found, for example, ‘h’.
- indexpos: This parameter specify an index position from where to start finding an element.
- string: This parameter specify a single character whose index position is to be found, for example ‘hello’.
- element: This parameter specify an element whose index position is to be found in an array.
The return value of the function is the index position of a given element.
Functions & Examples of lastIndexOf() Function
Below is the html code to understand the lastIndexOf() function more clearly with the example:
Example #1
We write the html code to understand the lastIndexOf() function more clearly when we search for the first character of a string, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title>This is an example for lastIndexOf() function</title>
</head>
<body>
<h3>JavaScript String object the lastIndexOf() function :</h3>
<script>
var str= "This is sample string.";
document.write(str.lastIndexOf('i')+ "<br />");
document.write(str.lastIndexOf('t')+ "<br />");
document.write(str.lastIndexOf('h')+ "<br />");
</script>
</body>
</html>
Output:
As in the above output, the last index of ‘i’ is 18,‘t’ is 16, and ‘h’ is 1.
Example #2
We write the html code to understand the lastIndexOf() function more clearly with the example where search for a character in a string by providing the start index position, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title> This is an example for lastIndexOf() function </title>
</head>
<body>
<h3>JavaScript String object the lastIndexOf() function :</h3>
<script>
var str = "This is sample string.";
document.write(str.lastIndexOf('i',6)+ "<br />");
document.write(str.lastIndexOf('t',10)+ "<br />");
document.write(str.lastIndexOf('h',0)+ "<br />");
</script>
</body>
</html>
Output:
As in the above output, the last index of ‘i’ is 5 because it starts finding from the 6th index in reverse order(up to 6 index); hence ‘i’ is found at the 5th index, ‘t’ is -1 because ‘t’ is not found up to 10 indexes and ‘h’ is -1 same ‘h’. is not found up to 0 index.
Example #3
We write the html code to understand the lastIndexOf() function more clearly when we search for the first character of a string, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title> This is an example for lastIndexOf() function </title>
</head>
<body>
<h3>JavaScript String object the lastIndexOf() function :</h3>
<script>
var str = "This is sample string.";
document.write(str.lastIndexOf("is")+ "<br />");
document.write(str.lastIndexOf("sample")+ "<br />");
document.write(str.lastIndexOf("hello")+ "<br />");
</script>
</body>
</html>
Output:
As in the above output, the last index of the first character of the string “is” is 5, “sample” is 8, and “hello” is -1 because the string is not found.
Example #4
We write the html code to understand the lastIndexOf() function more clearly, where we search for an element of a string by providing the start index position, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title> This is an example for lastIndexOf() function </title>
</head>
<body>
<h3>JavaScript String object the lastIndexOf() function :</h3>
<script>
var str = "This is sample string.";
document.write(str.lastIndexOf("is",4)+ "<br />");
document.write(str.lastIndexOf("sample",6)+ "<br />");
document.write(str.lastIndexOf("hello",16)+ "<br />");
</script>
</body>
</html>
Output:
As in the above output, the last index of the first character of the string “is” is 2 up to 4 index, “sample” is -1 because up to 6 index string is not found, and “hello” is -1 because is a string not found.
Example #5
We write the html code to understand the lastIndexOf() function more clearly, where we search for an element of an array by providing the start index position, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title>This is an example for lastIndexOf() function</title>
</head>
<body>
<h3>JavaScript Array object the lastIndexOf() function :</h3>
<script>
var array = ["Apple", "Banana", "Orange"];
document.write(array.lastIndexOf("Apple")+ "<br />");
document.write(array.lastIndexOf("orange")+ "<br />");
document.write(array.lastIndexOf("hello")+ "<br />");
</script>
</body>
</html>
Output:
As seen in the example output above, the last index of the element “Apple” is 0, while “orange” is -1. This is because the lastIndexOf() function is case-sensitive, meaning that “orange” is not the same as “Orange”. Additionally, “hello” is also -1 because it could not be found.
Example #6
We write the html code to understand the lastIndexOf() function more clearly, where we search for an element of an array by providing the start index position, as in the following example.
Code:
<!DOCTYPE html>
<head>
<title> This is an example for lastIndexOf() function </title>
</head>
<body>
<h3>JavaScript Array object the lastIndexOf() function :</h3>
<script>
var array = ["Apple", "Banana", "Orange"];
document.write(array.lastIndexOf("Apple",2)+ "<br />");
document.write(array.lastIndexOf("Orange",2)+ "<br />");
document.write(array.lastIndexOf("Banana",0)+ "<br />");
</script>
</body>
</html>
Output:
Above, we can see that the last index of the element “Apple” is 0 (within the given index position), “Orange” is 2, and “Banana” is -1 because the method could not find the element up to the given index.
Conclusion
JavaScript has a built-in function called lastIndexOf(). Developers utilize this function to perform various operations on both string and array objects. The lastIndexOf() returns the last index position of an element in a given string or array. Note that the lastIndexOf() function is case-sensitive.
Recommended Articles
This is a guide to JavaScript lastIndexOf(). Here we discuss the Introduction to JavaScript lastIndexOf() and its Parameters, along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –