TypeScript Tutorial #17 - Multiple Types in Array

preview_player
Показать описание
#TypeScript #Array

For programs to be useful, we need to be able to work with some of the simplest units of data: numbers, strings, structures, boolean values, and the like. In TypeScript, we support the same types as you would expect in JavaScript, with an extra enumeration type thrown in to help things along.

The use of variables to store values poses the following limitations −

Variables are scalar in nature. In other words, a variable declaration can only contain a single at a time. This means that to store n values in a program n variable declarations will be needed. Hence, the use of variables is not feasible when one needs to store a larger collection of values.

Variables in a program are allocated memory in random order, thereby making it difficult to retrieve/read the values in the order of their declaration.

TypeScript introduces the concept of arrays to tackle the same. An array is a homogenous collection of values. To simplify, an array is a collection of values of the same data type. It is a user-defined type.

TypeScript, like JavaScript, allows you to work with arrays of values. Array types can be written in one of two ways. In the first, you use the type of the elements followed by [] to denote an array of that element type.

let list: number[] = [1, 2, 3];

The second way uses a generic array type, Array lt elemType gt:

let list: Array lt number gt = [1, 2, 3];

gt --- Greater than sign
lt --- Less than sign

✅ Let's connect:

Twitter - @AnshulLaddha3

Instagram - /anshul_laddha
Рекомендации по теме