• JavaScript Video Tutorials

JavaScript - TypedArray at() Method



The JavaScript TypedArray at() method is used to retrieve a typed array element at the specified position (or index). It allows both positive and negative index values, where positive indices start counting from the beginning, and negative indices count from the end.

For example, the index value -1 represents the last element, whereas the 0 index value represents the first element in the typed array.

Syntax

Following is the syntax of JavaScript TypedArray at() method −

at(index)

Parameters

This method accepts a parameter named 'index', which is described below −

  • index − The zero-based index of the typed element is to be returned.

Return value

This method returns a typed array element found at the specified index.

Examples

Example 1

If we pass the index value as 2, it will start counting from the beginning (from the 0th index) and return the third element in the typed array.

In the following example, we are using the JavaScript TypedArray at() method to retrieve an element at the specified position (index) 2 in this typed array [1, 2, 3, 4, 5, 6, 7, 8].

<html>
<head>
   <title>JavaScript TypedArray at() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
      document.write("Typed array: ", T_array);
      const index = 2;
      document.write("<br>Index: ", index);
      document.write("<br>The index ", index, " represents the element ", T_array.at(index));
   </script>
</body>
</html>

Output

The above program returns 3 for index value 2.

Typed array: 1,2,3,4,5,6,7,8
Index: 2
The index 2 represents the element 3

Example 2

If the index value is passed as -1, it will return the last element of the typed array.

The following is another example of the JavaScript TypedArray at() method. We use this method to retrieve an element at the specified position -1 in this typed array [10, 20, 30, 40, 50].

<html>
<head>
   <title>JavaScript TypedArray at() Method</title>
</head>
<body>
   <script>
      const T_array = new Uint8Array([10, 20, 30, 40, 50]);
      document.write("Typed array: ", T_array);
      const index = -1;
      document.write("<br>Index: ", index);
      document.write("<br>The index ", index, " represents the element ", T_array.at(index));
   </script>
</body>
</html>

Output

After executing the above program, it will return the last element of the typed array as −

Typed array: 10,20,30,40,50
Index: -1
The index -1 represents the element 50

Example 3

In the given example, we create a function named returnSecondLast(), which accepts a typed array as a parameter. We use the at() method within this function to retrieve the second last element of this typed array [10, 20, 30, 40, 50].

<html>
<head>
   <title>JavaScript TypedArray at() Method</title>
</head>
<body>
   <script>
      function returnSecondLast(T_array){
         return T_array.at(-2);
      }
      const T_array = new Uint8Array([10, 20, 30, 40, 50]);
      document.write("TypedArray: ", T_array);
      
      //calling functions
      const secondLast = returnSecondLast(T_array);
      document.write("<br>Second last element: ", secondLast);
   </script>
</body>
</html>

Output

Once the above program is executed, it will return the second last element that is 40.

TypedArray: 10,20,30,40,50
Second last element: 40
Advertisements