How to Fill an Array with 1 in JavaScript?

Estimated read time 1 min read

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

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

Alternatively, you can use a for loop:

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

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply