Open In App

JavaScript typedArray.lastIndexOf() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 accepts 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 up to 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.

Example 1: 

javascript




// 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
console.log(b);
console.log(c);
console.log(d);
console.log(e);
console.log(f);
console.log(g);


Output:

6
4
3
-1
5
-1

Example 2: 

javascript




// 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
console.log(b);
console.log(c);
console.log(d);
console.log(e);
console.log(f);
console.log(g);


Output:

1
0
3
-1
-1
-1


Last Updated : 10 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads