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.
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
Please Login to comment...