HomeLanguagesJavascriptJavaScript typedArray.lastIndexOf() Method

JavaScript typedArray.lastIndexOf() Method

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS