JavaScript | typedArray.set() with Example

The typedArray.set() is an inbuilt function in JavaScript which is used to stores a number of values in the given typedArray.

typedArray.set(typedArray, offset)

Parameters: It accept two parameters which are specified below-

  • typedarray: It is the source array.
  • offset: It is optional and it is into the typedArray at which to begin setting the values. Its default values is zero (0).
  • Return value: It returns the new formed typedArray.
    JavaScript code to show the working of this function:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
      // Creating some buffers with sizes in bytes
      const buf1 = new ArrayBuffer(8);
      const buf2 = new ArrayBuffer(12);
      const buf3 = new ArrayBuffer(16);
      
      // Creating some typedArray
      const A = new Uint8Array(buf1);
      const B = new Uint8Array(buf2);
      const C = new Uint8Array(buf3);
      
      // Coping the values into the array
      // starting at index 3, 4, 5
      A.set([ 1, 2, 3, 4 ], 3);
      B.set([ 1, 2, 3, 5, 6 ], 4);
      C.set([ 1, 2 ], 5);
      
      // Priniting modified values
      document.write(A +"<br>");
      document.write(B +"<br>");
      document.write(C);
        
    </script>

    chevron_right

    
    

    Output:

    0,0,0,1,2,3,4,0
    0,0,0,0,1,2,3,5,6,0,0,0
    0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0


    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.