JavaScript | typedArray.copyWithin() with Examples

The typedArray.copyWithin() is an inbuilt function in JavaScript which is used to copy some elements of a typedArray at the specified location in the same typedArray.

Syntax:

typedArray.copyWithin(target, start, end)

Parameters: It accepts three parameters that are specified below-

  • target: It is the start index position from where to copy the element.
  • start: It is the start index position from where to start copying elements and its default value is starting index of the typedArray.
  • end: It is optional and it is the end position index till the elements to be copied and its default value is end of the typedArray.
  • Return value: It returns the modified array after copying process is done.

    <>JavaScript code to show the working of this function:

    Code #1:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
       // Constructing a new typedArray "A"
       // with some elements
       const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
      
       // Calling copyWithin function to copy 
       // element from index position 0 and
       // element from index 4 to 5
       A.copyWithin(0, 4, 5);
      
       // Printing a new modified array
       document.write(A);
         
    </script>

    chevron_right

    
    

    Output:

    25,10,15,20,25,30,35,40

    Code #2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
      
       // Constructing some new typedArrays
       // with some elements
       const A = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
       const B = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
       const C = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
       const D = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
       const E = new Uint8Array([ 5, 10, 15, 20, 25, 30, 35, 40 ]);
      
       // Calling copyWithin function with different
       // parameters
       a = A.copyWithin(0, 5);
       b = B.copyWithin(1, 4);
       c = C.copyWithin(0, 4, 5);
       d = D.copyWithin(2, 3, 5);
       e = E.copyWithin(0, 1, 4);
      
       // Printing new modified arrays
       document.write(a +"<br>");
       document.write(b +"<br>");
       document.write(c +"<br>");
       document.write(d +"<br>");
       document.write(e);
         
    </script>

    chevron_right

    
    

    Output:

    30,35,40,20,25,30,35,40
    5,25,30,35,40,30,35,40
    25,10,15,20,25,30,35,40
    5,10,20,25,25,30,35,40
    10,15,20,20,25,30,35,40


    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.