Objects

The Course
•
6 min read

An array gives items positions. An object gives values names. A study session has a topic, a duration, and a completion flag; an object keeps those facts together. Object literals use braces, property names followed by colons, and commas between entries.

session.topic reads a property. session.done = true changes one. A missing property evaluates to undefined; a misspelling such as session.minuts will not automatically report a typo. Meaningful names and small checks help you catch that kind of error.

An array of objects represents several records. Loop over the array and use dot access on each record. Avoid parallel arrays for topics and minutes: inserting into only one could silently pair the wrong topic with a duration.

null is an intentional empty value you choose. undefined often means a value is absent, such as a missing property. They are different from an empty string or the number zero. We will use explicit fields and booleans rather than guessing from absent data.

Predict before running

Which property changes, and which stay the same?

Predict, then run JavaScript
const session = { topic: "Loops", minutes: 25, done: false };

session.done = true;
console.log(session.topic);
console.log(session.done);
console.log(session.missing);
Check your prediction
Loops
true
undefined

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

Property names can be chosen at runtime

Dot access is convenient when you know the property name in your code. Bracket access uses an expression to choose the name. With field = "minutes", session[field] reads minutes, while session.field asks for a property literally named field.

Property names are case-sensitive. A missing key returns undefined, which can silently enter a calculation and produce NaN. Inspect the record and the spelling instead of covering up the result with a default immediately.

Property names can be chosen at runtime JavaScript
const session = { topic: "Loops", minutes: 25, done: false };
const field = "minutes";

console.log(session[field]);
console.log(session.field);
session.minutes = session.minutes + 5;
console.log(`${session.topic}: ${session.minutes}`);
Check your prediction
25
undefined
Loops: 30

The brackets evaluate field first. The dot uses the written property name literally.

When a variable name and property name are the same, JavaScript allows a shorter object notation: { topic, minutes } means { topic: topic, minutes: minutes }. Read the colon form first: the left side is the property name; the right side is the value to store. Later examples use both forms.

A record in this course is simply an object whose properties describe one item. It is not a separate JavaScript data type.

Choose a consistent record shape

If some records use duration and others use minutes, every calculation must guess which property to read. Pick a shape and keep it consistent. In our planner every session will have topic (string), minutes (number), and done (boolean). Missing data should be handled at the boundary where records enter the program.

Design task: sketch two records for a small library: title, pages, and finished. Then write a loop that prints unfinished titles. Explain why an array of records is safer than separate arrays of titles and page counts. What would happen if one list were sorted without the other?

Use an array when position and repeated records matter. Use an object when a small group of named fields describes one thing. You can combine the two instead of forcing all data into one structure.

Separate missing fields from meaningful values

A record groups related facts under names. A missing property reads as undefined, while a property can legitimately hold false, zero or empty text. Treating every falsy value as missing can erase a real answer such as zero completed minutes.

Property access and assignment operate on the same object through its reference. Reading a typo does not create that field. Assigning the typo does create a different field, so JavaScript may run while the intended field remains unchanged. Inspect the property names as carefully as the values.

False is still a stored answer JavaScript
const session = { topic: "Loops", minutes: 0, done: false };

console.log(session.minutes);
console.log(session.done);
console.log(session.note === undefined);
Check your prediction
0
false
true

Zero minutes and false completion are present data. Only note is missing here. A branch like if(session.minutes) would answer a truthiness question, not whether the property exists.

A misspelling can silently add a field JavaScript
const session = { topic: "Arrays", done: false };

session.complete = true;
console.log(session.done);
console.log(session.complete);
session.done = true;
console.log(session.done);
Check your prediction
false
true
true

Complete and done are different property names. The first assignment did not update done. This is a behavior bug, even though both statements are valid JavaScript and no exception is thrown.

Try the rule on new inputs

Add a note and read it with dot and bracket notation. Then deliberately misspell minutes, inspect the result, and repair the access. Explain why a missing field should not automatically become a valid zero-minute value.

Your turn

Exercise

Objects: practise

For the supplied records, print only completed topics, one per line. Expected: Loops. Then mark Arrays complete and confirm both appear.

Your working space JavaScript
const sessions = [
  { topic: "Loops", done: true },
  { topic: "Arrays", done: false }
];
// Print the completed topics.
Hint 1: the idea

The array holds records; the done property determines which records to show.

Hint 2: a first step

Loop with for (const session of sessions), then check session.done.

Show one solution
const sessions = [
  { topic: "Loops", done: true },
  { topic: "Arrays", done: false }
];

for (const session of sessions) {
  if (session.done) {
    console.log(session.topic);
  }
}
Loops

Each condition checks an actual boolean. The topic and completion status remain attached to the same record as it travels through the loop.

What you should be able to explain

  • Explain session[field] versus session.field.
  • Design a record with three clearly named properties.
  • How can inconsistent property names become a logic error rather than a syntax error?

Was this article helpful?

Your response is saved on this device.