×

typescript · level 2 · unit 1 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

An keeps related facts together under one name.

So far each box held one value. Real data comes in bundles: a pet has a name, an age and a favourite toy. An object bundles them in curly brackets, like { name: "Rex", age: 3 }. Each part is a property: a name, a colon and a value.

Read a property with a dot, like rex.name, the same dot you used for .length. Change one the same way: rex.age = 4;. Even in a const box the properties can change. Only the box itself is locked.

The type label for an object lists each property and its type: { name: string; age: number }. Ask for a property that is not on the list and the checker stops you with TS2339.

const rex: { name: string; age: number } = { name: "Rex", age: 3 };
console.log(rex.name);
rex.age = 4;
console.log(`${rex.name} is now ${rex.age}.`);
console.log(rex);

When you show a whole object, each property name appears in quotes, with no spaces.