Changing a value
To change what a dictionary stores under a key, assign a new value to that key. The old value is replaced, and every other pair stays as it was.
const scores: { [key: string]: number } = {"Alice": 95, "Bob": 87};
scores["Alice"] = 99;
console.log(scores); // { Alice: 99, Bob: 87 }
The same syntax does two jobs. If the key already exists, the assignment updates it. If it doesn't, the assignment adds a new pair:
scores["Charlie"] = 72;
console.log(scores); // { Alice: 99, Bob: 87, Charlie: 72 }
Because both cases look identical, a typo in a key silently creates a new pair instead of updating the one you meant. After scores["Alise"] = 99, the dictionary has both Alice with its old score and a new Alise.
const and the contents of an object
The dictionary above is declared with const, and the assignments still work. The keyword const fixes which object the variable refers to. It doesn't make the object read-only. What you can't do is point the name at a different object:
const d: { [key: string]: number } = {"x": 1};
d["x"] = 100; // fine: changes the object's content
d["y"] = 200; // fine: adds a key
// d = {}; // error: cannot assign to a const variable
console.log(d); // { x: 100, y: 200 }
Dot notation does the same thing for simple names: d.x = 100 is equivalent to d["x"] = 100. With the dictionary type shown above, the compiler accepts both forms and any new string key.
Types decide which keys you can add
An object written without a type annotation gets a type with exactly the keys it was created with. Adding another key by dot notation is then reported as an error:
const d = {x: 1};
d.x = 100; // fine
d.y = 200; // the compiler reports: property 'y' does not exist on this type
The JavaScript that the compiler writes still adds y when it runs, but the error means the type doesn't match what the program does. Annotating the variable as { [key: string]: number } or Record<string, number>, as the lesson does, states that new keys are expected.
The type of the new value
The annotation also checks what you assign. A dictionary of numbers rejects a string value:
scores["Alice"] = "ninety-nine"; // error: a string is not a number
The compiler reports this at compile time. Once the types are erased, nothing at run time checks them, which is one more reason to read the compiler's messages.
Updating based on the old value
You can read and write in one statement. The compound operators work on dictionary entries like on any variable:
const scores: { [key: string]: number } = {"Alice": 95, "Bob": 87};
scores["Bob"] += 5;
console.log(scores["Bob"]); // 92
If the key doesn't exist, this is a trap. The missing value is undefined, and undefined + 5 gives NaN, so scores["Zed"] += 5 stores NaN under a new key. Start a counter from zero with scores["Zed"] = (scores["Zed"] ?? 0) + 5.
Shared dictionaries and removing pairs
An object is passed around by reference. If a second variable refers to the same dictionary, an update through either name is visible through both:
const same = scores;
same["Alice"] = 0;
console.log(scores["Alice"]); // 0
There is only one dictionary, with two names. To remove a pair entirely, use delete scores["Bob"]. Assigning undefined is not the same: the key would still be there, holding undefined, and it would still be counted among the object's keys. After delete the key is gone, and reading it gives undefined as for any key that was never added.
The lesson's exercise
The starting code declares scores with Alice at 95 and Bob at 87. The exercise has one step: below the dictionary, change Alice's score to 99 with an assignment to scores["Alice"].
const scores: { [key: string]: number } = {"Alice": 95, "Bob": 87};
scores["Alice"] = 99;
The key is written exactly as it appears in the dictionary, with a capital A and in quotes. Afterwards scores holds { Alice: 99, Bob: 87 }. Don't redeclare the dictionary: a second const scores is an error, and an assignment is all that is needed.
Key takeaways
obj["key"] = valueupdates the pair if the key exists and adds it if not.- A
constdictionary can be changed, but the name can't be reassigned to another object. - A misspelled key creates a new pair instead of an error.
- The annotation controls which keys and value types the compiler accepts.