Start learning free

LESSON 127 OF 675 · DICTIONARIES

Updating Dictionary Values in TypeScript

Lesson

You can update an existing value in a TypeScript dictionary (an object with string keys) by assigning a new value to an existing key:

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

scores["Alice"] = 99;     // update existing key
scores["Charlie"] = 72;   // add new key

Note: even though scores is declared with const, you can still modify its properties — you just can't reassign the variable itself.

const d: { [key: string]: number } = {x: 1};
d.x = 100;    // x is now 100
d.y = 200;    // new key "y" added

Assignment
Follow the Coding Tutorial and update Alice's score to 99!


Hint
Use scores["Alice"] = 99.

STEP 1 OF 1
    Ctrl + Enter to check