In JavaScript, 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 = {};
scores["Alice"] = 95;
scores["Bob"] = 87;
Assignment
Follow the Coding Tutorial and create an object scores with three student scores!
Hint
Use const scores = {"Alice": 95, "Bob": 87, "Charlie": 72};.
Free plan: 30 tutor messages. PRO is unlimited.
