Tutorialspoint.dev

JavaScript | Array lastIndexOf()

arr.lastIndexOf() function is used to find the index of the last occurrence of the search element provided as the argument to the function. The syntax of the function is as follows:

arr.lastIndexOf(searchElement[,index])

Arguments The first argument to this function is the searchElement which is the value to be searched in the array. Second argument to this function is the optional index argument which defines the starting index in the array from where the element is to be searched backwards. If this argument is not provided then index arr.length – is taken as the starting index to begin the backward search as it is the default value.

Return value This function returns the index of the last occurrence of the searchElement. If the element cannot be found in the array, then this function returns -1

Examples for the above function are provided below:

Example 1:



var array = [2, 9, 9];
print(array.lastIndexOf(2));

Output:

0

In this example the function lastIndexOf() returns the last index of occurrence of 2 which is 0

Example 2:

var array = [2, 98, 12, 45];
print(array.lastIndexOf(45,2));

Output:

-1

In this example the function lastIndexOf() finds the last index of occurrence of 45. Since it is not present starting frm index 2, therefore the function returns -1.
Example 3:

var array = [2, 98, 12, 45];
print(array.lastIndexOf(98,2));

Output:

1

In this example the function lastIndexOF() returns the last occurrence of 98 as 1.

Example 4:

var array = [2, 98, 12, 45];
print(array.lastIndexOf(100));

Output:

-1

In this example the function lastIndexOF() checks the last occurrence of 100. Since it is not present therefore it returns -1



Example 5:

var array = [2, 98, 12, 98];
print(array.lastIndexOf(98));

Output:

3

Codes for the above function are provided below:

Program 1:

<script>
// JavaScript to illustrate lastIndexOf() 
  
function func() {
  
    // Original array
    var array = [2, 9, 9];
    document.write(array.lastIndexOf(2));
}
  
func();
</script>

Output:

0

Program 2:

<script>
// JavaScript to illustrate lastIndexOf() 
  
function func() {
  
    //Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(45,2));
}
  
func();
</script>

Output:

-1

Program 3:

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    // Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(98,2));
}
  
func();
</script>

Output:

1

Program 4:

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    // Original array
    var array = [2, 98, 12, 45];
    document.write(array.lastIndexOf(100));
}
func();
</script>

Output:

-1

Program 5:

<script>
// JavaScript to illustrate lastIndexOf() function
  
function func() {
  
    //Original array
    var array = [2, 98, 12, 98];
    document.write(array.lastIndexOf(98));
}
func();
</script>

Output:

3


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter