Arrays and Tuples
Type an array
T[] or Array<T> both mean an array of T.
const scores: number[] = [88, 92, 76];
scores.push(95);
console.log(scores.length);
Map and filter with types
Callback parameters are often inferred from the array element type.
const names = ["Ada", "Lin", "Sam"];
const short = names.filter((name) => name.length <= 3);
console.log(short);
Use a tuple for fixed positions
A tuple gives each index its own type. Useful for pairs like coordinates or CSV-like rows.
const point: [number, number] = [10, 20];
const entry: [string, number] = ["lessons", 18];
console.log(point[0], entry[1]);
Readonly when needed
readonly number[]prevents push and assignment- Tuples can be
readonly [string, number] - Prefer arrays when length varies
- Prefer tuples when position carries meaning
Comments
One comment per signed-in account. Comments are saved with this page’s URL.