We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Tuples vs. Objects

So why would you have to use a tuple instead of an object? For example, this:

function getCoordinates(): [number, number] {
  return [40.7128, -74.006]; // latitude, longitude
}

Instead of this:

function getCoordinatesAsObject(): { lat: number; lng: number } {
  return { lat: 40.7128, lng: -74.006 };
}

Coordinates have a conventional order (latitude, then longitude), so a tuple's positional semantics fit well. Objects are clearer for named access (coords.lat).

Counterexample

I would probably model a User as an object:

type User = { name: string; age: number; email: string };
const user: User = { age: 60, name: "Lane", email: "[email protected]" };

A tuple like [string, number, string] would be confusing – user[0] for name? user[2] for email? Objects' descriptive keys are more intuitive.