• JavaScript Video Tutorials

JavaScript - TypedArray forEach() Method



The JavaScript TypedArray forEach() method executes a provided function once for each element in a typed array and returns None(undefined).

Here are some additional points you should know about the 'forEach()' method −

  • The forEach() method operates on a TypedArray (such as Uint8Array, Int16Array, etc.).

  • It accepts a testing function as a parameter.

  • The testing function is executed for each element in the TypedArray.

Syntax

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

forEach(callbackFn, thisArg)

Parameters

This method accepts two parameters named 'callbackFn' and 'thisArg', which are described below −

callbackFn − This parameter is a testing function that will be executed for each element in the TypedArray.

This function takes three parameters named 'element', 'index', and 'array'. Here's the description for each parameter −

  • element − Represents the current element being processed in the TypedArray.

  • index − Indicates the index (position) of the current element within the TypedArray.

  • array − Refers to the entire TypedArray.

thisArg (optional) − This is an optional parameter that allows you to specify the value of this within the callbackFn.

Return value

This method returns None(undefined).

Examples

Example 1

In the following program, we use the JavaScript TypedArray forEach() method to execute the provided function once for each element. We create an arrow function that iterates through each element of this typed array [10, 20, 30, −40, −50, 60] and passes it as an argument to this method.

<html>
<head>
   <title>JavaScript TypedArray forEach() Method</title>
</head>
<body>
   <script>
      const T_array = new Int16Array([10, 20, 30, -40, -50, 60]);
      document.write("Typed array: ", T_array);
      
      //using forEach() method
      document.write("<br>The typed array elements: ")
      T_array.forEach((element)=>{
         document.write(element);
      });
   </script>
</body>
</html>

Output

The above program returns the each element of typed array as −

Typed array: 10,20,30,-40,-50,60
The typed array elements: 102030-40-5060

Example 2

The following is another example of the JavaScript TypedArray forEach() method. We use this method to execute the provided function named display() once for each typed array element. We pass this function as an argument to this method, and the function checks the index and values of the typed array element and passes it as an argument to this method.

<html>
<head>
   <title>JavaScript TypedArray forEach() Method</title>
</head>
<body>
   <script>
      function display(element, index, array){
         document.write("<br>a[",index,"]", " = ", element);
      }
      const T_array = new Int16Array([10, 20, 30, -40, -50, 60]);
      document.write("Typed array: ", T_array);
      
      //using forEach() method
      T_array.forEach(display);
   </script>
</body>
</html>

Output

After executing the above program, it returns indexes and values of typed array elements.

Typed array: 10,20,30,-40,-50,60
a[0] = 10
a[1] = 20
a[2] = 30
a[3] = -40
a[4] = -50
a[5] = 60

Example 3

As we had discussed earlier, the forEach() method doesn't return a value. Let's verify it with a suitable example −

<html>
<head>
   <title>JavaScript TypedArray forEach() Method</title>
</head>
<body>
   <script>
      function display(element, index, array){
         return element > 0;
      }
      const T_array = new Int16Array([10, 20, 30, -40, -50, 60]);
      document.write("Typed array: ", T_array);
      
      //using forEach() method
      document.write("<br>The forEach() method returns: ", T_array.forEach(display));
   </script>
</body>
</html>

Output

The above program returns "undefined".

Typed array: 10,20,30,-40,-50,60
The forEach() method returns: undefined
Advertisements