EN
JavaScript - fill array with default values (ES6+)
0 points
In this article, we would like to show you how to fill array with default values in JavaScript (ES6+).
Quick solution:
xxxxxxxxxx
1
const array = Array(3).fill(0);
2
3
console.log(array); // [ 0, 0, 0 ]
In this example, we create an array
with size 5
and use fill()
method to fill all its elements with 1
as default value.
xxxxxxxxxx
1
const array = Array(5).fill(1);
2
3
console.log(array); // [ 1, 1, 1, 1, 1 ]