JavaScript 09 · Store Pip’s state in an object

JavaScript lesson 9 of 50 · Beginner · About 30 minutes

Keep Pip’s name, location, energy and tools together in one mission record.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Create an object and change or read its properties with dot notation.

Get ready

Open Complete checkpoint below. Paste the JavaScript into a blank JavaScript editor and run it.

Group the details. Use a single object to hold Pip’s state. → Update one property. Change the location and energy after moving. → Check the nested list. Read the toolkit inside Pip’s object.
Group the details → Update one property → Check the nested list

Understand the JavaScript in this lesson

Language to look for object literals, properties, keys and values, dot notation, changing properties, arrays inside objects.

1. Give Pip one record

Start with const pip and create an object with braces. An object is a group of named pieces of information. Each line inside it matches a key to a value with a colon. A comma separates each pair from the next. The toolkit value is an array inside the object. Storing the values together is a way to describe Pip’s state, the robot’s situation right now.

const pip = {
  name: "Pip",
  location: "Central Hub",
  energy: 12,
  toolkit: ["torch", "spanner"]
};

2. Change details of the same robot

Use dot notation to ask for one property, such as pip.location. The value on the right of an assignment then becomes that property’s new value. pip.energy -= 2 means subtract two from the energy and write back the result. On the toolkit line, JavaScript first finds the array with pip.toolkit, then uses its push() method. The const name keeps pointing at this one object throughout the changes.

console.log(`Before the move: ${pip.name} at ${pip.location}, energy ${pip.energy}.`);
pip.location = "Workshop";
pip.energy -= 2;
pip.toolkit.push("wind cell");

3. Check before and after

Place the final messages below the changes. The first report in the program already read the original state, so it shows Central Hub and 12 energy. The final reports read the updated properties, so they show Workshop and 10 energy. The last membership check confirms that the array recorded the discovery even though it lives inside another value.

console.log(`After the move: ${pip.location}, energy ${pip.energy}.`);
console.log("Tools packed:", pip.toolkit.length);
console.log("Wind cell packed:", pip.toolkit.includes("wind cell"));
Follow what happens step by step
  1. Create the object
    Location Central Hub, energy 12, two tools.Why Each property gives that part of Pip’s state a name.
  2. Run the move and pickup statements
    Location Workshop, energy 10, and wind cell added.Why Each statement changes a specific part of Pip’s record.
  3. Read the toolkit length
    Tools packed: 3Why The dot reaches the toolkit array, and its length is now three.

Predict before you run Can you change pip.energy while pip was declared with const?

Reveal the answer

Yes. The property can change while the name pip continues to refer to the original object.

Complete checkpoint

JavaScript · the instructions

const pip = {
  name: "Pip",
  location: "Central Hub",
  energy: 12,
  toolkit: ["torch", "spanner"]
};

console.log(`Before the move: ${pip.name} at ${pip.location}, energy ${pip.energy}.`);
pip.location = "Workshop";
pip.energy -= 2;
pip.toolkit.push("wind cell");

console.log(`After the move: ${pip.location}, energy ${pip.energy}.`);
console.log("Tools packed:", pip.toolkit.length);
console.log("Wind cell packed:", pip.toolkit.includes("wind cell"));

Read the JavaScript line by line

Open a small group of lines while you read the complete checkpoint. Blank lines separate ideas. Closing braces mark the end of a block. An ellipsis here means you can read the rest in the full program.

Lines 1–15
Lines 1–6 const pip = { name: "Pip", location: "Central Hub", energy: 12, toolkit: ["torch", "spanner"] };
Set up pip. Keep this name attached to the same value. Braces create an object. Properties give names to the stored values. Square brackets create an ordered array. Its first position is 0.
Line 8 console.log(`Before the move: ${pip.name} at ${pip.location}, energy ${pip.energy}.`);
Show a message or value in the output. Use it to watch the game work.
Line 9 pip.location = "Workshop";
Store the result in the name or property to the left.
Line 10 pip.energy -= 2;
Update the existing value with the operation shown.
Line 11 pip.toolkit.push("wind cell");
Add entries to the end of the array and return its new length.
Line 13 console.log(`After the move: ${pip.location}, energy ${pip.energy}.`);
Show a message or value in the output. Use it to watch the game work.
Line 14 console.log("Tools packed:", pip.toolkit.length);
Show a message or value in the output. Use it to watch the game work.
Line 15 console.log("Wind cell packed:", pip.toolkit.includes("wind cell"));
Show a message or value in the output. Use it to watch the game work.

Mission challenge

Add a rescued property with a value of false. After Pip reaches the Workshop, print its value and explain why it has not changed.

Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.