Changing an entry
A dictionary is not frozen after you create it. To change the value stored under a key, assign a new value to that key. The old value is replaced:
const scores = {"Alice": 95, "Bob": 87};
scores["Alice"] = 99; // update an existing key
scores["Charlie"] = 72; // add a new key
console.log(scores); // { Alice: 99, Bob: 87, Charlie: 72 }
The same assignment syntax does two jobs. If the key already exists, the assignment updates it. If the key doesn't exist, the assignment creates it. JavaScript does not ask which one you meant, and it does not report an error either way. That is convenient, but it also means a typo in a key quietly adds a new, unwanted entry instead of failing.
Why a const object can change
The lesson declares scores with const, yet the assignments above work. This is not a contradiction. const fixes the variable, so it can never be pointed at another object. It does not make the object's contents read-only:
const d = {x: 1};
d.x = 100; // x is now 100
d.y = 200; // new key "y" added
console.log(d); // { x: 100, y: 200 }
// d = {}; // TypeError: Assignment to constant variable.
The commented-out line tries to reassign the variable itself, and that is what const forbids. If you ever do need an object that cannot be changed, Object.freeze(d) exists, but the lesson doesn't need it.
Dot notation and bracket notation
Both ways of naming a key work on the left of an assignment. scores["Alice"] = 99 and scores.Alice = 99 do the same thing. Bracket notation is the one to use when the key is stored in a variable or contains a space:
const who = "Bob";
scores[who] = 90; // updates the key "Bob"
As with reading, case matters. scores["alice"] = 99 would not update "Alice". It would add a second key named "alice".
Computing a new value from the old one
Because reading and writing use the same notation, you can update a value based on what it holds now. Compound operators such as += work on dictionary entries too:
scores["Bob"] += 5;
console.log(scores["Bob"]); // 92
This reads the current value, adds 5, and writes the result back to the same key. Be careful with a key that doesn't exist yet. undefined + 5 is NaN, so += on a missing key stores NaN, not 5. Counting code usually writes counts[key] = (counts[key] ?? 0) + 1 to start from zero safely.
Removing an entry
Updating can add or replace, but it can't remove. For that, use the delete operator: delete scores["Bob"] removes the key and its value completely. Setting a value to undefined is not the same. The key stays in the object with the value undefined, and it still counts as an entry.
The exercise, step by step
The starter code already has the object:
const scores = {"Alice": 95, "Bob": 87};
The task is to change Alice's score to 99.
- Name the key being updated:
scores["Alice"]. It exists already, with the value 95. - Assign the new value with a single equals sign:
= 99. The old value 95 is replaced.
No let or const is needed, because you are not declaring anything. You are changing something that exists.
The solution in JavaScript
const scores = {"Alice": 95, "Bob": 87};
scores["Alice"] = 99;
Afterwards the object is { Alice: 99, Bob: 87 }. Bob's entry is untouched.
Common mistakes
- Adding a keyword. Writing
let scores["Alice"] = 99is a syntax error. A property assignment is a plain statement. - Reassigning the whole object.
scores = {"Alice": 99}fails on aconst, and even where it worked it would discard Bob. - Using
==instead of=.scores["Alice"] == 99only compares and changes nothing. - Misspelling the key. The code runs without error and creates a new key, while the real one keeps its old value.
Key takeaways
- Assigning to a key replaces its value if the key exists and adds the key if it doesn't.
constprotects the variable, not the contents of the object.- Use bracket notation when the key is in a variable. Keys are case sensitive.
deleteremoves an entry. Assigningundefineddoes not.