Lesson 2 of 15

Arrays & Tuples

Arrays

Type a JavaScript array with Type[]:

const scores: number[] = [95, 87, 92, 78];
const tags: string[] = ["typescript", "javascript"];
const flags: boolean[] = [true, false, true];

TypeScript ensures every element matches the declared type:

const scores: number[] = [1, 2, "three"];  // Error: 'string' is not assignable to 'number'

An alternative syntax uses Array<Type>:

const scores: Array<number> = [1, 2, 3];

Tuples

A tuple is a fixed-length array where each position has a known type:

const point: [number, number] = [3, 4];
const entry: [string, number] = ["Alice", 30];

console.log(point[0]);   // 3
console.log(entry[0]);   // Alice
console.log(entry[1]);   // 30

Tuples are useful when a function needs to return multiple values of different types.

Your Task

Create a scores array of type number[] containing [95, 87, 92, 78, 100]. Print the first score, the last score, and the length.

TypeScript loading...
Loading...
Click "Run" to execute your code.