Objects & collections
Objects { }
A set of named values: one variable, many related fields.
Why it exists
Data in programs is almost always "compound": a user has a name, an age and an email; a product has a name, a price and a quantity. Keeping them in separate variables is awkward — they drift apart and get mixed up. An object gathers them under one name, and the keys explain what's what: order.price is clearer than order[1].
The object's second role is a dictionary: values can be looked up by a key computed while the program runs — say, a counter for each letter or a setting by its name.
Where it's used
- Data from an API or a database: users, orders, products —
{ id, name, price }. - Function options:
fetch(url, { method: "POST" }). - Dictionaries and counters:
{ cat: 2, dog: 1 }. - Settings and configs:
{ theme: "dark", fontSize: 14 }.
Syntax
const object = { key: value, otherKey: value };
object.key
object["key"]
"key" in objectExamples
const user = { name: "Anna", age: 30 };
user.name; // → "Anna"const key = "price";
const item = { name: "Tea", price: 200 };
item[key]; // → 200
item.key; // → undefinedconst user = { name: "Anna" };
"name" in user; // → true
"email" in user; // → false
user.email; // → undefinedin checks whether a key existsconst name = "Ben";
const age = 25;
const person = { name, age };
person; // → { name: "Ben", age: 25 }Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
An object is created with the literal { }: inside, key: value pairs are listed with commas. Keys are strings (quotes can be dropped if the key looks like a variable name); values are anything, including other objects and arrays.
Reading and writing. user.name reads the name property. Square brackets user[key] do the same, but the name comes from the expression's value — that's how keys from variables work. Assigning user.email = "…" creates a new property or changes an existing one.
A missing property reads as undefined — no error. So "does the key exist" is checked with "email" in user or Object.hasOwn(user, "email"), not by the value.
Shorthand. { name, age } is the same as { name: name, age: age }. { [key]: value } creates a property with a computed name.
An object is held by reference: const b = a doesn't copy the object; it gives the same object a second name.
Traps
How it differs from…
- Indexes and
length An array stores values in order and reaches them by number; an object by name. A list of similar things is an array; a description of one thing from different fields is an object.
- Property access with
[] The dot and square brackets in detail are in a separate entry.
When to pick something else
If the keys aren't known in advance, there are many of them and they're often added and removed (a cache, counters for arbitrary values), a Map fits better: it has size, its keys needn't be strings, and it has no inherited keys like toString. For an ordered list of similar values, use an array.