Languages
[Edit]
EN

TypeScript - fill array with default values

0 points
Created by:
Imaan-Morin
1009

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 ]

Alternative titles

  1. TypeScript - fill whole array with specified value
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join