JavaScript | typedArray.fill()

The typedArray.fill() is an inbuilt function in JavaScript which is used to fill a value to typedArray from start index to end index.
Syntax:

typedarray.fill(value, start, end)

Parameters: It takes three parameters that are specified below-

  • value: It is the value to fill with typed array.
  • start: It is starting index, optional and its default value is 0.
  • end: It is ending index, optional and not included. Its default value is length of the typedArray.
  • Return value: It returns the modified array.
    JavaScript code to show the working of this function:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Javascript to illustrate typedArray.fill()
    <script>
      
       // Creating some typedArrays
       const A = new Uint8Array([ 0, 0, 0, 0 ]);
       const B = new Uint8Array([ 0, 0, 0, 0 ]);
       const C = new Uint8Array([ 0, 0, 0, 0 ]);
       const D = new Uint8Array([ 0, 0, 0, 0 ]);
      
       // Calling fill() function to fill different values
       a = A.fill(4, 1, 3);
       b = B.fill(5, 1);
       c = C.fill(3);
       d = D.fill(4, 0, 0);
      
       // Printing modified array
       document.write(a +"<br>");
       document.write(b +"<br>");
       document.write(c +"<br>");
       document.write(d +"<br>");
         
    </script>

    chevron_right

    
    

    Output:

    0,4,4,0
    0,5,5,5
    3,3,3,3
    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.