The Javascript TypedArray fill() method fills all the elements of an array from start index to the end index with a static value.
Syntax:
array.fill(value) array.fill(value, start) array.fill (value, start, end)
Parameters:
value: It represents the value to fill the array.
start: It represents index position to start filling the array. Default is 0.
end: It represents the index position to end filling the array. Default is array.length -1.
Returns:
Size of the array without modification.
Example 1:
<!DOCTYPE html> <html> <body> <script type="text/javascript"> var Jewels = ["DIAMOND","GOLD","PLATINUM","SILVER"]; Jewels.fill("GOLD"); document.write(Jewels); </script> </body> </html> |
Output:
GOLD,GOLD,GOLD,GOLD |
Example 2:
<!DOCTYPE html> <html> <body> <script type="text/javascript"> const testArray = new Uint8Array([34, 45, 23, 10]); testArray.fill(4, 1, 3); document.write(testArray); </script> </body> </html> |
Output:
34,4,4,10 |
Please Share