Open In App

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

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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];
 
// 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 ]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads