Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | typedArray.lastIndexOf() with Examples

  • Last Updated : 03 Aug, 2018
Geek Week

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:




<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>

Output:

6
4
3
-1
5
-1

Code #2:




<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>

Output:

1
0
3
-1
-1
-1

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :