How to use Array.push() method In Javascript
In this example, the Array.push() method is used to add elements to the end of the array. The for loop is used to repeat the push method 10 times to add the value 0 to the array.
let myArray = [];
for (let i = 0; i < 10; i++) {
myArray.push(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.