• JavaScript Video Tutorials

JavaScript - TypedArray lastIndexof() Method



The JavaScript TypedArray lastIndexOf() method searches for a specified searchElement in a typed array from right to left. It returns the index of the last occurrence of the searchElement and -1 if not found.

Suppose the specified searchElement is present only once in the typed array, then this method returns the index starting from the zeroth index. If the searchElement is present more than once, it will give the first priority to the last element.

Syntax

Following is the syntax of JavaScript TypedArraylastIndexof() method −

lastIndexOf(searchElement, fromIndex)

Parameters

This method takes in two parameters, namely 'searchElement' and 'fromIndex'. The parameters are described below −

  • searchElement − The element that needs to be searched in the typed array.

  • fromIndex − The zero-based index indicates the position to start searching from the end.

Return value

This method returns the last index of the searchElement in a typed array. If the search element is not present, it returns −1.

Examples

Example 1

If the fromIndex parameter is omitted and only the searchElement parameter is passed, the method returns the last index of the specified searchElement from the end of the TypedArray.

In the following example, we are using the JavaScript TypedArray method lastIndexOf() to retrieve the last index of the specified searchElement 30 in the typed array [10, 20, 30, 40, 30, 50, 60].

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 30, 40, 50, 60]);
      document.write("Original typed array: ", T_array);
      const serachElement = 30;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement));
   </script>    
</body>
</html>

Output

The above program returns the index of the last occurrence of the element 30 as 3.

Original typed array: 10,20,30,30,40,50,60
Search element: 30
The element 30 found at the last index 3

Example 2

If we pass both 'searchElement' and 'fromIndex' parameters to this method, it returns the last index of the element starting at the specified fromIndex.

The following is another example of the JavaScript TypedArray lastIndexOf() method. We are using this method to retrieve the last index of the specified element 6, starting the search from index 5, in this typed array: [1, 2, 4, 6, 8, 9, 6, 11].

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2 , 4, 6, 8, 9, 6, 11]);
      document.write("Original typed array: ", T_array);
      const serachElement = 6;
      const fromIndex = 5;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>From index: ", fromIndex);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement, fromIndex));
   </script>    
</body>
</html>

Output

After executing the program, the index of the last occurrence of element 6 is returned as 3.

Original typed array: 1,2,4,6,8,9,6,11
Search element: 6
From index: 5
The element 6 found at the last index 3

Example 3

If the specified searchElement is not found in this typed array, it returns -1.

In this example, we are using the typed array lastIndexOf() method on a typed array to retrieve the last index of the searchElement 11 in the given typed array [1, 3, 5, 7, 13].

<html>
<head>
   <title>JavaScript TypedArray lastIndexof() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array( [1, 3, 5, 7, 13]);
      document.write("Original typed array: ", T_array);
      const serachElement = 6;
      document.write("<br>Search element: ", serachElement);
      document.write("<br>The element ", serachElement, " found at the last index ", T_array.lastIndexOf(serachElement));
   </script>    
</body>
</html>

Output

Once the above program is executed, it will return -1.

Original typed array: 1,3,5,7,13
Search element: 6
The element 6 found at the last index -1
Advertisements