JavaScript | typedArray.forEach() with Examples

The typedArray.forEach() is an inbuilt function in JavaScript which executes a given function once for each given typedArray’s element.
Syntax:

typedarray.forEach(callback)

Parameter: It accepts a parameter “callback” function which produces each element of the new typedArray and this function takes three parameter that are specified below-

  • currentValue: It is the value of the current element which is being processed in the typedArray.
  • index: It is the index of the current element being processed in the array.
  • array: It is the array on which forEach() function is being called.

Return Value: It does not return anything.
JavaScript code to show the working of this function:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Constructing some typedArray
    const A = new Uint8Array([ 1, 3, 5, 7 ])
    const B = new Uint8Array([ 2, 4, 6, 8 ])
  
      // Calling callback function
      function callback(element, index, array) {
          document.write('a[' + index + '] = ' + element + "<br>");
      }
  
      // Calling forEach() function
      A.forEach(callback);
      B.forEach(callback);
  
</script>

chevron_right


Output:

a[0] = 1
a[1] = 3
a[2] = 5
a[3] = 7
a[0] = 2
a[1] = 4
a[2] = 6
a[3] = 8


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.