EN
TypeScript - fill array with default values
0 points
In this article, we would like to show you how to fill an array with default values in TypeScript.
Quick solution:
xxxxxxxxxx
1
const array: number[] = Array(3).fill(0);
2
3
console.log(array); // [ 0, 0, 0 ]
In the below examples, we create an array
with size 3
and use fill()
method to fill all its elements with the default values.
xxxxxxxxxx
1
const array: number[] = Array(3).fill(1);
2
3
console.log(array); // [ 1, 1, 1 ]
xxxxxxxxxx
1
const array: string[] = Array(3).fill('A');
2
3
console.log(array); // [ 'A', 'A', 'A' ]
xxxxxxxxxx
1
const array: boolean[] = Array(3).fill(false);
2
3
console.log(array); // [ false, false, false ]