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:
// ONLINE-RUNNER:browser;
const array = Array(3).fill(0);
console.log(array); // [ 0, 0, 0 ]
Practical example
In this example, we create an array
with size 5
and use fill()
method to fill all its elements with 1
as default value.
// ONLINE-RUNNER:browser;
const array = Array(5).fill(1);
console.log(array); // [ 1, 1, 1, 1, 1 ]