What is the use of fill() method in JavaScript Arrays ?
Last Updated :
31 Jan, 2024
The fill()
method in JavaScript is used to fill all the elements of an array with a static value. It modifies the original array and returns the modified array.
Example: Here, the fill(0)
method is applied to the myArray
, which replaces all elements in the array with the value 0
.
Javascript
let myArray = [1, 2, 3, 4, 5];
myArray.fill(0);
console.log(myArray);
|
Share your thoughts in the comments
Please Login to comment...