How to Fill a JavaScript Array with Zeros?

Estimated read time 1 min read

To fill an array with a single repeated value of 0 in JavaScript, you can use the Array.from method with a map function:

const size = 10;
const arr = Array.from({length: size}, () => 0);
console.log(arr);

Alternatively, you can use a for loop:

const size = 10;
const arr = [];
for (let i = 0; i < size; i++) {
  arr.push(0);
}
console.log(arr);

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply