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:
const array: number[] = Array(3).fill(0);
console.log(array); // [ 0, 0, 0 ]
Practical examples
In the below examples, we create an array with size 3 and use fill() method to fill all its elements with the default values.
const array: number[] = Array(3).fill(1);
console.log(array); // [ 1, 1, 1 ]
const array: string[] = Array(3).fill('A');
console.log(array); // [ 'A', 'A', 'A' ]
const array: boolean[] = Array(3).fill(false);
console.log(array); // [ false, false, false ]