⊗jstsPmBsAr 6 of 55 menu

Arrays in TypeScript

Arrays in TypeScript are strongly typed, meaning they can only contain data of one type.

The data type of an array is determined in two ways. Let's examine them.

The first way

Let's make an array with strings. To do this, after the variable name, we specify the data type, and after it, we write square brackets to indicate that we have an array:

let arr: string[] = ['a', 'b', 'c', 'd', 'e'];

Let's print some element of the array:

console.log(arr[0]); // will bring out 'a'

The second way

There is an alternative way to declare an array. In it, we specify the keyword Array, and then specify the data type in angle brackets. See the example:

let arr: Array<string> = ['a', 'b', 'c', 'd', 'e'];

Practical tasks

Using the first method, specify the data type in the following array:

let arr = [1, 2, 3, 4, 5];

Using the second method, specify the data type in the following array:

let arr = [1, 2, 3, 4, 5];
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline