Open In App

What is the use of fill() method in JavaScript Arrays ?

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.




let myArray = [1, 2, 3, 4, 5];
 
// Fill the entire array with the value 0
myArray.fill(0);
 
console.log(myArray); // Output: [0, 0, 0, 0, 0]

Output
[ 0, 0, 0, 0, 0 ]
Article Tags :