Objects as Records

Naming the parts instead of numbering them

A list gives every value a number: the first slot, the second slot, and so on. That is exactly right when the values are all the same kind of thing. It falls apart when they are not, because slot 1 of a list tells you nothing about what slot 1 is for.
An object groups named fields that belong together. The whole thing sits inside braces, and each field is written as a name, a colon, and a value, with commas between them.
let car = { make: "Tesla", model: "X", year: 2017 };
One variable now holds three separate facts about one car, and every one of them carries a name that says what it is. A record like this is a single value: it can be passed to a function, returned from one, or stored in a list, and all three fields travel together.

Brackets, for when the name is not fixed

A dot needs the field name written out, letter for letter, at the moment you type the code. Brackets do not. car["year"] reads the very same field as car.year, and there are two situations where only the brackets will do.
• The name is held in a variable. car[wanted] reads whichever field wanted is naming right now, and that can change while the program runs.
• The name is not a legal variable name. A field may be called "top speed" or "2019 owner", and neither of those can follow a dot.

Fields can be added later

An object is not sealed shut once it is created. Assigning to a field name that was never in the braces does not fail; it makes that field.
car.owner = "Ada";
console.log(car.owner);

Counting the fields

There is no .length on an object. car.length is an ordinary field read like any other, and since nothing in the braces is called length, it produces undefined. To count fields, ask for the field names and take the length of that:
console.log(Object.keys(car).length);
After the owner line above, that prints 4.
What you haveHow to reach it
The field name, typed outcar.year
The field name held in a variablecar[wanted]
A field name with a space in itcar["top speed"]
No such field at allundefined, and no error
How many fields there areObject.keys(car).length

const freezes the binding, not the contents

This is the part that catches people who arrive from another language.
const makes one promise, and it is a promise about the NAME: the name will never be pointed at a different value. It says nothing whatsoever about the value itself. So the fields of a const object can be read, changed and added to freely, and const has no opinion about any of it.
The program below reads fields of one record with a dot and with brackets. It also reads a field that is not there, adds it, and counts the fields. Run it, then make each change below and run it again. Press Reset before each change.
• In let choice = "kind";, change "kind" to "age".
• Change pet[choice] to pet["choice"].
• Change let pet to const pet and run it. Then add the line pet = { name: "Pepper" }; at the end and run it again.
let pet = { name: "Biscuit", kind: "beagle", age: 4 };
console.log("Name:", pet.name);
let choice = "kind";
console.log(choice + ":", pet[choice]);
console.log("Owner:", pet.owner);
pet.owner = "Mira";
console.log("Owner:", pet.owner);
console.log("Fields:", Object.keys(pet).length);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Destructuring pulls fields out by name

Writing car.make over and over gets noisy fast. Destructuring copies fields into variables of the same name in a single line.
let { make, year } = car;
console.log(make);
console.log(year);
The braces on the LEFT are not building an object. They are a shopping list of field names. Each name on the list is matched to the field that has that name, so the order you write them in makes no difference at all: let { year, make } = car; does exactly the same job.
If you have met a language where unpacking goes by position, this is where the two part company: there the order on the left is the whole point, and here it is not, because the names carry the meaning. That is also why a name matching no field arrives holding undefined instead of throwing anything.