Functions - JavaScript lastIndexOf()
Posted by Superadmin on May 07 2023 07:38:42

JavaScript lastIndexOf()

By Afshan BanuAfshan Banu
  

JavaScript lastIndexOf()

Introduction to JavaScript lastIndexOf() Function

lastIndexOf() function in javascript is used for different purposes. The lastIndexOf() function can used on string and array for different tasks. The lastIndexOf() function is used to return the starting position of the particular word or character in a given string. The lastIndexOf() function is used to return the index position of particular element in the given array.  The lastIndexOf() function is a built in function in javaScript. This function is same as indexOf() function in javaScript, but the difference is it start finding an element from the last position of the string or an array.

The lastIndexOf() function return index position always starts with zero of first element in a string or an array. If an element is not found in the string or an array, then return will be

Syntax of JavaScript lastIndexOf()

There are different syntax to use the lastIndexOf() function depending on the tasks and parameter passed, as given below:

1. The Syntax of lastIndexOf() function for string:

2. The Syntax of lastIndexOf() function for array:

Parameters of JavaScript lastIndexOf()

Below are the Parameters of JavaScript lastIndexOf():

Return value of the function is the index position of a given element.

Functions & Examples of lastIndexOf() Function

Below are 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 where 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:

JavaScript lastIndexOf() Example 1

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 character in a string with providing the start index position, as in 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:

JavaScript lastIndexOf() Example 2

As in the above output the last index of ‘i’ is 5 because it start finding from 6th index in reverse order(up to 6 index) hence ‘i’ is found at 5th  index ,’t’ is -1 because ‘t’ is not found up to 10 index 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 where we search for first character of a string, as in 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:

JavaScript lastIndexOf() Example 3

As in the above output the last index of first character of string “is” is 5, “sample” is 8 and “hello” is -1 because string not found.

Example #4

We write the html code to understand the lastIndexOf() function more clearly where we search for an element of an string with providing the start index position, as in 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:

JavaScript lastIndexOf() Example 4

As in the above output the last index of first character of 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 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 with providing the start index position, as in 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:

Array Object Example 5

As in the above output the last index of  an element “Apple” is 0, “orange” is -1 because the lastIndexOf() function is case sensitive hence “orange” is not same as “Orange” and “hello” is -1 because element not found.

Example #6

We write the html code to understand the lastIndexOf() function more clearly where we search for an element of an array with providing the start index position, as in 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:

Array Object Example 6

As in the above output the last index of an element “Apple” is 0 (with in given index position) , “Orange” is 2 and “Banana” is -1 because element not found up to given index.

Conclusion

The lastIndexOf() function is a built in function in javaScript, which used to perform different tasks on 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.