JavaScript | typedArray.lastIndexOf() with Examples

The typedArray.lastIndexOf() is an inbuilt function in JavaScript which is used to return the last index of the typedArray at which a given element is present otherwise it returns -1 if the element is not present.
Syntax:

typedArray.lastIndexOf(search_Element, from_Index])

Parameters: It accept two parameters which are specified below:

  • search_Element: It is the element whose last index is to be searched.
  • from_Index: It is optional. It is the index upto which an element is to be searched and its default value is the length of the typedArray.

Return value: It returns the last index of the typedArray at which a given element is present otherwise it returns -1 if the element is not present.

JavaScript code to show the working of above function:

Code #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Creating a typedArray with some elements
  var A = new Uint8Array([5, 10, 15, 15, 5, 20, 10]);
    
  // Calling lastIndexOf() function
  b = A.lastIndexOf(10);     
  c = A.lastIndexOf(5);     
  d = A.lastIndexOf(15);  
  e = A.lastIndexOf();  
  f = A.lastIndexOf(20); 
  g = A.lastIndexOf(25); 
    
  // Printing returned values
  document.write(b +"<br>");
  document.write(c +"<br>");
  document.write(d +"<br>");
  document.write(e +"<br>");
  document.write(f +"<br>");
  document.write(g +"<br>");
    
</script>

chevron_right


Output:

6
4
3
-1
5
-1

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
  // Creating a typedArray with some elements
  var A = new Uint8Array([5, 10, 15, 15, 5, 20, 10]);
    
  // Calling lastIndexOf() function
  b = A.lastIndexOf(10, 1);     
  c = A.lastIndexOf(5, 2);     
  d = A.lastIndexOf(15, 3);  
  e = A.lastIndexOf(15, 1);  
  f = A.lastIndexOf(20, 1); 
  g = A.lastIndexOf(25, 3); 
    
  // Printing returned values
  document.write(b +"<br>");
  document.write(c +"<br>");
  document.write(d +"<br>");
  document.write(e +"<br>");
  document.write(f +"<br>");
  document.write(g +"<br>");
    
</script>

chevron_right


Output:

1
0
3
-1
-1
-1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.