Start learning free

LESSON 125 OF 675 · DICTIONARIES

Dictionaries (Objects) in TypeScript

Lesson

In TypeScript, objects serve as dictionaries — they store key-value pairs where each key maps to a value.

const scores = {"Alice": 95, "Bob": 87, "Charlie": 72};

Objects use curly braces {}. Keys and values are separated by colons :, and pairs are separated by commas. You can also use a Map for more dictionary-like behavior, but objects are most common.

const person = {"name": "Alice", "age": 25, "city": "NYC"};
console.log(person["name"]);            // Alice
console.log(Object.keys(person).length); // 3

You can add new key-value pairs after creation:

const scores: Record<string, number> = {};
scores["Alice"] = 95;
scores["Bob"] = 87;

Assignment
Follow the Coding Tutorial and create an object scores with three student scores!


Hint
Use const scores: Record<string, number> = {"Alice": 95, "Bob": 87, "Charlie": 72};.

STEP 1 OF 1
    Ctrl + Enter to check