28.3. Arrays in TypeScript

PHOTO EMBED

Wed Aug 31 2022 20:50:21 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

//Arrays in TypeScript must contain values of the same type. When declaring an array, the type needs to be declared.
let arrayName: number[] = [10,9,8];
//What if the array needs to hold values of different types? Now, we need a tuple. A tuple is a special structure in TypeScript that can hold as many values as needed of different types.

let tupleName: [number, string, number];

tupleName = [10, "9", 8];


// JS=let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];

let cargoHold: string[] = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket'];

//JS=
let element1 = ['hydrogen', 'H', 1.008];
let element2 = ['helium', 'He', 4.003];
let element26 = ['iron', 'Fe', 55.85];

//TS=
let element1: [string, string, number];
element1 = ['hydrogen', 'H', 1.008];

let element2: [string, string, number];
element2 = ['helium', 'He', 4.003];

let element26: [string, string, number];
element26 = ['iron', 'Fe', 55.85];
content_copyCOPY