JavaScript 02 · Give Pip a mission status

JavaScript lesson 2 of 50 · Beginner · About 20 minutes

Use names for Pip’s tools and keep track of a location that can change.

A distress signal lights up Pip’s antenna at Sky Dock while two cloud creatures wait on a distant platform.
Mission Choose when to use const or let and predict the current value of a named variable.

Get ready

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

Name fixed details. Pip and the mission get const names. → Name the changing detail. Use let for Pip’s current location. → Update and read. Move to Workshop and print the current value.
Name fixed details → Name the changing detail → Update and read

Understand the JavaScript in this lesson

Language to look for const, let, variable names, assignment, reassignment, comma.

1. Name the details that stay fixed

Rather than writing Pip’s name in every message, store the string once under rover. The keyword const declares a name that cannot be reassigned to a different value. The = sign gives the name its value. Pick clear names so you can later read a sentence like “show the rover”. Variable names are case-sensitive, so rover and Rover refer to different names.

const rover = "Pip";
const task = "Restore the wind beacon";
let location = "Central Hub";

2. Read the value using its name

Put the printing instructions below the declarations. In the first call, "Rover:" is a fixed string. After the comma, rover is a name without quotes. JavaScript looks up its current value and gives both pieces to console.log(). Writing "rover" there would show the word rover instead of looking up Pip’s name.

console.log("Rover:", rover);
console.log("Task:", task);
console.log("Starting location:", location);

3. Update the detail that changes

The program first gives location the value "Central Hub" with let because Pip can move. Add the Workshop lines last. The new assignment uses the same name without writing let again. JavaScript now uses the new location whenever a later instruction reads it. Move the last print statement before the assignment and check which value appears.

location = "Workshop";
console.log("Current location:", location);
Follow what happens step by step
  1. Read the declarations
    rover is Pip, task is the rescue goal, and location is Central Hub.Why The assignment on each line connects a name to a value.
  2. Run the first location call
    Starting location: Central HubWhy Pip has not moved yet.
  3. Assign the Workshop location
    Current location: WorkshopWhy Later uses of a let name see its most recent assigned value.

Predict before you run What happens if you change let location to const location at the top?

Reveal the answer

The first three messages still work. The attempt to assign Workshop raises an error, so JavaScript does not reach the last print.

Complete checkpoint

JavaScript · the instructions

const rover = "Pip";
const task = "Restore the wind beacon";
let location = "Central Hub";

console.log("Rover:", rover);
console.log("Task:", task);
console.log("Starting location:", location);

location = "Workshop";
console.log("Current location:", location);

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–10
Line 1 const rover = "Pip";
Set up rover. Keep this name attached to the same value.
Line 2 const task = "Restore the wind beacon";
Set up task. Keep this name attached to the same value.
Line 3 let location = "Central Hub";
Set up location. Give this name a starting value that later code can change.
Line 5 console.log("Rover:", rover);
Show a message or value in the output. Use it to watch the game work.
Line 6 console.log("Task:", task);
Show a message or value in the output. Use it to watch the game work.
Line 7 console.log("Starting location:", location);
Show a message or value in the output. Use it to watch the game work.
Line 9 location = "Workshop";
Store the result in the name or property to the left.
Line 10 console.log("Current location:", location);
Show a message or value in the output. Use it to watch the game work.

Mission challenge

After the Workshop report, set location to Garden and print it again. Keep rover and task fixed.

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