Base CS from zero
Objects as key-value
An array is the right shape when your values form a sequence — position 0, position 1, position 2 — and the positions themselves carry no meaning beyond order. But much of the data a program handles is not like that. A user has a name, an age, an email. These are not “element 0, element 1, element 2”. Each one has a meaning, and that meaning is a word, not a number.
For data like this, languages give you the object: a value made of named fields.
Instead of reaching a value by counting positions, you reach it by its name. user.age
says “the age field of user” directly.
This lesson shows what an object is in memory — a set of named slots — and traces how the program reaches a field by its name. You already understand the array; the object is its counterpart, and seeing them side by side makes both clearer.
After this lesson you can define an object as a set of key-value pairs, explain what a field, a key, and a value are, trace how the program reads a field by its name, and state the core difference between an array and an object — positional index versus named key.
An object is a set of named fields. Recall from Unit 06 that a variable is a named cell: a name bound to a slot in memory that holds a value. An object takes that idea and bundles many named cells together under one value.
An object is made of fields (also called properties). Each field has two parts:
- A key — the field’s name, written as a word, like
nameorage. - A value — the data stored in that field, like
"Mara"or30.
A key paired with its value is a key-value pair. An object is simply a collection of
key-value pairs. You can picture each field as a labelled slot: the label is the key, and
the slot holds the value. The object { name: "Mara", age: 30 } has two fields — a slot
labelled name holding "Mara", and a slot labelled age holding 30.
Reaching a value: name, not position. To read a field you write object.key — for
example user.age. The program takes the key age, finds the slot in the object that
carries that label, and reads the value in it. The key is the address into the object.
Array versus object — the core contrast. Both group several values under one name. The difference is how you reach a value inside:
- An array reaches a value by position:
arr[2]means “the element at index 2”. The positions are numbers, and their order is the whole structure. (Recall: the program finds it by computing an address from the index.) - An object reaches a value by name:
obj.agemeans “the field labelled age”. The fields have word labels, and their order does not matter —{ a: 1, b: 2 }and{ b: 2, a: 1 }are the same object.
Use an array when your data is an ordered sequence of same-kind values. Use an object when your data is a fixed set of differently-meaning, named pieces.
The program below builds an object with three fields and reads two of them by name.
1
// An object literal: three named fields (key-value pairs).
2
let user = {
3
name: "Mara", // key "name" -> value "Mara"
4
age: 30, // key "age" -> value 30
5
active: true, // key "active" -> value true
6
};
7
8
// Read a field BY ITS NAME, not by a position:
9
let a = user.age; // find the slot labelled "age", read 30
10
let n = user.name; // find the slot labelled "name", read "Mara"
11
12
// Compare with an array, which is reached BY POSITION:
13
let nums = [10, 20, 30];
14
let x = nums[1]; // element at index 1 -> 20
- L2 An object: a set of key-value pairs grouped under the name user
- L3 Field 1 — key 'name' labels a slot holding the value 'Mara'
- L4 Field 2 — key 'age' labels a slot holding the value 30
- L9 user.age: look up the slot whose label is 'age', read its value (30)
- L13 An array reaches values by numeric position, not by name
- L14 nums[1]: position 1, computed as an offset from the base — see lesson 02
Step through building user and reading two fields. Each cell is one field of the object;
the label below each cell is that field’s key.
1
let user = {
2
name: "Mara",
3
age: 30,
4
active: true,
5
};
6
let a = user.age;
7
let n = user.name;
Why this works
Why use names instead of positions for this kind of data? Numeric positions only carry
meaning when the data is genuinely a sequence. For a user record, position 0 being the
name and position 1 being the age would be an arbitrary convention you would have to
memorise and never break. A key is self-describing: user.age says exactly what it reads.
Names also free the data from order — adding a new field does not shift the others, the
way inserting an array element shifts every later index. Named fields trade the array’s
tight positional layout for clarity and flexibility.
Common mistake
A common mistake is to think an object’s fields have a meaningful order, like an array’s
elements. They do not. { a: 1, b: 2 } and { b: 2, a: 1 } describe the same object: the
same set of key-value pairs. You never reach a field by “which one comes first” — you
reach it by its key. If the order of items matters to your program, that is a signal you
want an array, not an object.
The object { name: "Mara", age: 30, active: true } has how many fields (key-value pairs)?
let p = { x: 7, y: 4 }; The program reads p.y. What value does it read?
let item = { price: 50, qty: 3 }; The program reads item.price. What value does it read?
An object is reached by name; an array is reached by numeric position. To get the value of a field called 'score', do you use a numeric index? Type 1 for yes, 0 for no.
let a = { v: 1, w: 1 }; let b = { w: 1, v: 1 }; The fields are the same key-value pairs in a different written order. Do a and b describe the same object's set of fields? Type 1 for yes, 0 for no.
What is an object, and how is it different from an array?
An object is a value made of fields (also called properties), where each field is
a key-value pair: a key is the field’s name, and the value is the data stored
under it. You can picture each field as a labelled slot — the key is the label, the slot
holds the value. To read a field you use its key: user.age finds the slot labelled age
and reads its value. The core difference from an array: an array reaches a value by
numeric position (arr[2]), while an object reaches a value by name (obj.age).
An object’s fields therefore have no meaningful order — { a: 1, b: 2 } and
{ b: 2, a: 1 } are the same object. Use an array for an ordered sequence of same-kind
values; use an object for a named set of differently-meaning pieces.