BeginnerCoder

Lesson 10 — Readonly & tuples

Fixed-length, fixed-type arrays

A tuple is an array where each position has its own type and the length is fixed: [number, number] is exactly two numbers, [string, number] is a name then a number. Adding readonly in front stops anyone from changing the items.

const entry: readonly [string, number] = ["Ann", 31];
// entry[1] = 32; ← error: readonly

Why it matters: coordinates, key/value pairs and React's useState result are all tuples.

Want it another way?
Task

Type point as a readonly tuple of two numbers, then print the sum of its two positions → 7.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.