How to use Array.fill() method In Javascript
One of the most common ways is to use the Array.fill() method. This method modifies the original array and fills it with a given value for a specified number of times.
Example : The Array.fill() method is used to fill an array with a specified value for a specified number of times. In this example, the Array.fill() method is used to fill an array of length 10 with the value 0.
let myArray = new Array(10);
myArray.fill(0);
console.log(myArray);
Output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
How to fill an array with given value in JavaScript ?
In JavaScript, an array is a collection of elements that can be of any data type. Sometimes, it is necessary to fill an array with a specific value for a given number of times. For example, an array of length 10 needs to be filled with the value 0, or an array of length 5 needs to be filled with the value “Hello”.
Approach: There are several ways to fill an array with a given value in JavaScript.