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:

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

Let's print some element of the array:

typescriptconsole.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:

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

Practical tasks

Задание 1

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

javascriptlet arr = [1, 2, 3, 4, 5];
Задание 2

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

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